import bisect,collections,copy,heapq,itertools,math,string,sys,queue,time,random input = lambda: sys.stdin.readline().rstrip() def I(): return input() def IS(): return input().split() def II(): return int(input()) def IIS(): return map(int,input().split()) def LIIS(): return list(map(int,input().split())) def Base_n_to_10(X,n): out = 0 for i in range(1,len(str(X))+1): out += int(X[-i])*(n**(i-1)) return out#int out def Base_10_to_n(X, n): if (X//n): return Base_10_to_n(X//n, n)+str(X%n) return str(X%n) INF=10**18 MOD=10**9+7 sys.setrecursionlimit(10**8) ############################################################################## p,q,r,k=IIS() li=[0 for i in range(10**5)] li[0]=p%10 li[1]=q%10 li[2]=r%10 i=3 while True: li[i]=(li[i-1]+li[i-2]+li[i-3])%10 if li[i]==r%10 and li[i-1]==q%10 and li[i-2]==p%10: #i-2こ break i+=1 li=li[:i-2] print(li[k%(i-2)-1])