def prod(ma, mb, mod = 10**9+7): n = len(ma) ret = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): for k in range(n): ret[i][j] += ma[i][k]*mb[k][j] ret[i][j] %= mod return ret def powmat(ma, k, mod = 10**9+7): n = len(ma) ret = [[0]*n for _ in range(n)] for i in range(n): ret[i][i] = 1 for _ in range(k): if k & 1: ret = prod(ret, ma, mod) ma = prod(ma, ma, mod) k >>= 1 if k == 0: break return ret p, q, r, k = map(int, input().split()) A = [[1, 1, 1], [1, 0, 0], [0, 1, 0]] Ak = powmat(A, k-3, 10)[0] ret = 0 ret += Ak[0] * r ret += Ak[1] * q ret += Ak[2] * p ret %= 10 print(ret)