p, q, r, k = map(int,input().split()) a = [0] * 10000 a[0] = p % 10 a[1] = q % 10 a[2] = r % 10 for i in range(3, 100): x = (a[i - 1] + a[i - 2] + a[i - 3]) % 10 if x in a[4:]: start = a.index(x) end = i a[i] = x break a[i] = x if k < end: print(a[k - 1]) exit() ind = (k - start) % (end - start) + start - 1 print(a[ind])