import numpy as np def mat_power(m: np.ndarray, n: int, mod: int) -> np.ndarray: if n == 0: return np.identity(len(m), dtype=object) res = mat_power(m, n // 2, mod) res = res @ res % mod if n % 2 == 1: res = res @ m % mod return res MOD = 10 P, Q, R, K = map(int, input().split()) P %= 10 Q %= 10 R %= 10 m = np.array( [[1, 1, 1], [1, 0, 0], [0, 1, 0]]) a = np.array( [[R], [Q], [P]]) t = mat_power(m, K-3, MOD) @ a ans = t[0, 0] % MOD print(ans)