import numpy as np MOD = 10 def pow_matrix_mod(x, n, mod=MOD): x = x.astype(object) if not n: return np.eye(len(x), dtype=object) if n % 2 == 0: return pow_matrix_mod(x @ x % mod, n // 2) % mod else: return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod *x, K = map(int, input().split()) M = np.array([[0, 0, 1], [1, 0, 1], [0, 1, 1]]) y = np.array(x) @ pow_matrix_mod(M, K - 3) % MOD print(y[-1])