import numpy as np p, q, r, K = map(int, input().split()) xs = [[1,1,1],[1,2,2],[2,3,4]] X = np.array(xs, dtype="object") S = np.array([p,q,r], dtype="object") K,m=divmod(K,3) for _ in range(60): if K%2: S = np.dot(X, S)%10 K = K//2 X = np.dot(X, X)%10 S = np.array(S, dtype="int64") print(S[(m-1)%3])