#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
	int a;
	while(scanf("%d",&a),a){
		int z=(long long)a*a*a;
		int ret=2000000000;
		for(int i=1;i<a;i++){
			int b=(int)(pow(z-(i*i*i),1.0/3)+0.000000001);
			ret=min(ret,z-i*i*i-b*b*b);
		}
		printf("%d\n",ret);
	}
}