#include <iostream>
using namespace std;

typedef long long ll;
const long long INF = 2147483647;

int main(){
    while( true ){
        ll z;
        cin >> z;
        if( !z ) break;
        ll zzz = z*z*z;
        ll min = INF;
        for(ll x = 1; x < z; x++){
            ll d = INF;
            ll xxx = x*x*x;
            for(ll y = z-1; y > 0; y--){
                d = zzz - xxx - y*y*y;
                if( d > 0 ) break;
            }
            if( d < min ) min = d;
        }
        cout << min << endl;
    }
    return 0;
}