/**
 * AOJ #1109: Fermat's Last Theorem (ICPC Domestic 2000)
 */

#include <stdio.h>

#define CUBE(x) ((x)*(x)*(x))

/**
 * Compute z^3 - max{ x^3+y^3 | x>0, y>0, x^3+y^3 <= z^3 }.
 */
int fermat(int z)
{
  int x, y, max = 0;
  for (x = 1; x < z; x++) {
    for (y = x; CUBE(x) + CUBE(y) <= CUBE(z); y++) {
      if (max < CUBE(x) + CUBE(y)) {
          max = CUBE(x) + CUBE(y);
      }
    }
  }
  return CUBE(z) - max;
}

int main()
{
  int z;
  for (;;) {
    scanf("%d", &z);
    if (z == 0) break;
    printf("%d\n", fermat(z));
  }
  return 0;
}