import numpy as np p, q, r, K = map(int, input().split()) K -= 3 A = np.array([[1, 1, 1], [1, 0, 0], [0, 1, 0]]) A % 10 l = np.array([r, q, p]) R = 0 for i in range(60): if (K >> i) & 1 == 1: l = A @ l l = l % 10 A = A @ A A = A % 10 print(l[0] % 10)