#include<stdio.h>

int main(void){
  int i,j;
  int z,z3;
  int max,tmp;

  while(1){
    scanf("%d",&z);
    if(!z)break;

    max = 0;
    z3 = z*z*z;
    for(i=z-1;i>0;i--){
      for(j=i;j>0;j--){
	tmp = i*i*i + j*j*j;
	if(tmp > max && tmp <= z3)max = tmp;
      }
    }

    printf("%d\n",z3 - max);

  }

  return 0;
}