#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
  int z;
  while (scanf("%d", &z), z) {
    int m = -1;
    for (int x = 1; x < z; ++x) {
      int lo = 1, hi = z;
      while (lo < hi) {
        int y = (lo + hi + 1) / 2;
        if (x*x*x + y*y*y <= z*z*z)
          lo = y;
        else
          hi = y-1;
      }
      m = max(m, x*x*x + lo*lo*lo);
    }
    printf("%d\n", z*z*z - m);
  }
}