#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    long long n;
    vector<long long> y3;
    for (long long y = 0; y < 1234; y++) {
	y3.push_back(y * y * y);
    }
    while (cin >> n, n) {
	long long ans = 1LL << 60;
	for (long long x = 1; x < n; x++) {
	    long long y = upper_bound(y3.begin(), y3.end(), n*n*n-x*x*x) - y3.begin();
	    if (y == 0 || y > n) continue;
	    y--;
	    ans = min(ans, n*n*n-x*x*x-y*y*y);
	}
	cout << ans << endl;
    }
    
    return 0;
}


