p,q,r,K = map(int,input().split()) K -= 1 d = {} x = (p%10,q%10,r%10) for i in range(10**3+1): p,q,r = x if x in d.keys(): break else: d[x] = i d[i] = x x = (q,r,(p+q+r)%10) #print(d) T = i-d[x] #print(d[x]) if K < d[x]: print(d[K][0]) else: K -= d[x] print(d[d[x] + K%T][0])