#include <iostream>
#include <cmath>

using namespace std;

int main() {
	long long int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		long long int MAX = 0;
		for (long long int i = 1; i < n; i++) {
			for (long long int j = i; j < n; j++) {
				if (i*i*i+j*j*j <= n*n*n) {
					MAX = max(MAX, i*i*i+j*j*j);
				} else {
					break;
				}
			}
			if (i*i*i*2 >= n*n*n) {
				break;
			}
		}
		cout << n*n*n-MAX << endl;
	}
	return 0;
}