import sys import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline def from_read(dtype=np.int64): return np.fromstring(read().decode(), dtype=dtype, sep=' ') def from_readline(dtype=np.int64): return np.fromstring(readline().decode(), dtype=dtype, sep=' ') def read_matrix(N, M, dtype=np.int64): matrix = np.empty((N, M), dtype=dtype) for i in range(N): matrix[i] = from_readline(dtype) return matrix def matrix_pow(A, N): res = np.eye(len(A), dtype=np.int64) while N: if N & 1: res = res @ A % 10 A = A @ A % 10 N >>= 1 return res def solve(p, q, r, K): A = np.array([[1, 1, 1], [1, 0, 0], [0, 1, 0]]) B = matrix_pow(A, K - 3) res = B[0, 0] * r + B[0, 1] * q + B[0, 2] * p res %= 10 return res def main(): p, q, r, K = from_readline() print(solve(p, q, r, K)) if __name__ == "__main__": main()