#include<iostream>
#include<climits>
#include<algorithm>
using namespace std;
#define C(x) (x*x*x)
int main()
{
	long long x,y,z,r;
	while(cin>>z,z)
	{
		r=LLONG_MAX;
		for(x=1;C(x)<=C(z);++x)for(y=x;C(x)+C(y)<=C(z);++y)r=min(r,C(z)-C(x)-C(y));
		cout<<r<<endl;
	}
	return 0;
}