p,q,r,k = map(int,input().split()) k -= 4 p %= 10 q %= 10 r %= 10 l = [[p,q,r]] vis = [] for i in range(10): vis.append([[0] * 10 for _ in range(10)]) vis[p][q][r] = 1 cnt = 1 start = 0 leg = 10 ** 3 + 1 for i in range(min(10 ** 3 + 1,k)): o_l = l[i][1::] l.append(o_l + [sum(l[i])% 10]) x,y,z = l[i+1] if vis[x][y][z] != 0: leg = cnt + 1 - vis[x][y][z] start = vis[x][y][z] break cnt += 1 vis[x][y][z] = cnt k = k - start amari = k % leg print(sum(l[start + amari]) % 10)