import sys sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline f_inf = float('inf') MOD = 10 ** 9 + 7 def solve(): p, q, r, k = map(int, input().split()) p, q, r = map(lambda x: x % 10, [p, q, r]) MAX = 1001 dp = [0] * MAX dp[0], dp[1], dp[2] = p, q, r cycle = 0 for i in range(3, MAX): cycle += 1 dp[i] = (dp[i - 3] + dp[i - 2] + dp[i - 1]) % 10 if (dp[0], dp[1], dp[2]) == (dp[i - 2], dp[i - 1], dp[i]): break k %= cycle print(dp[k - 1]) if __name__ == '__main__': solve()