p, q, r, K = map(int, input().split()) A = [0] * 1000 A[0] = p % 10 A[1] = q % 10 A[2] = r % 10 def conv(a, b, c): return (a % 10) * 100 + (b % 10) * 10 + c % 10 t = set() t.add(conv(A[0], A[1], A[2])) for k in range(3, 1000): A[k] = (A[k - 1] + A[k - 2] + A[k - 3]) % 10 t.add(conv(A[k], A[k - 1], A[k - 2])) print(A[(K - 1) % (len(t) - 1)])