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

#define MAX_N 2000

long long n;
long long maxn;

int main() {
	while (cin >> n) {
		maxn = 9999999999;
		if (n == 0) { break; }
		for (long long i = 1; i <= MAX_N; i++) {
			for (long long j = 1; j <= MAX_N; j++) {
				long long v = i*i*i + j*j*j;
				if (n*n*n >= v) {
					maxn = min(maxn, n*n*n - v);
				}
			}
		}
		cout << maxn << endl;
	}
}