### constant

MAX_Z = 1111

### subroutines

def lower_bound(arr, key, i0 = 0, i1 = arr.length)
  lb = i0 - 1
  ub = i1

  while lb + 1 < ub
    m = (lb + ub) / 2
    if arr[m] >= key
      ub = m
    else
      lb = m
    end
  end
  ub
end

### main

cbs = (0..MAX_Z).map{|z| z * z * z}

loop do
  z = gets.to_i
  break if z == 0
  #p z

  max_x3y3 = 0
  z3 = cbs[z]

  for x in (1...z)
    x3 = cbs[x]
    y = lower_bound(cbs, z3 - x3) - 1
    y3 = cbs[y]
    x3y3 = x3 + y3 
    max_x3y3 = x3y3 if max_x3y3 < x3y3
    #p [z3, x3, y3]
  end

  puts (z3 - max_x3y3)
end