ant = 0
def fun(mid):
global ant
now = next = 1
count = 1
while now < k :
next = now + 1
while next <= k and postion[next] - postion[now] < mid:
next += 1
ant = now
count += 1
now = next
if postion[next] - postion[ant] < mid :
count -= 1
return count >= n
n,k,p0 = map(int,input().strip().split())
postion = [0]*1000000
postion[1] = p0
for i in range(2,k+1):
postion[i] = postion[i-1] + ((postion[i-1]*2357 + 137) % 10) + 1
left = 1
right = postion[k] - postion[1]
ans = 0
while left <= right :
mid = (left + right)//2
if fun(mid):
left = mid + 1
ans = mid
else:
right = mid -1
print(ans)