from collections import deque p, q, r, K = map(int, input().split()) K -= 3 p, q, r = p % 10, q % 10, r % 10 que = deque() vis = set() for _ in range(1000): now = p * 100 + q * 10 + r if now in vis: break vis.add(now) que.append(now) p, q, r = q, r, (p + q + r) % 10 while que[0] != now and K: que.popleft() K -= 1 K %= len(que) while K: que.popleft() K -= 1 print(que[0] % 10)