#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;


int main(){
    int z;
    while(cin>>z,z){
        ll z3=z*z*z;
        ll ans=0;
        rep(i,z){
            rep(j,z){
                ll xy3=i*i*i+j*j*j;
                if(z3>=xy3) ans=max(ans,xy3);
            }
        }
        cout<<z3-ans<<endl;
    }
    return 0;
}