p, q, r, k = read_line.split.map(&.to_i64) k -= 3 p, q, r = {p, q, r}.map { |x| x % 10 } count = [nil.as(Int64?)] * 1000 array = [] of Int64 k.times do |now| x = p*100 + q*10 + r if cnt = count[x] cycle = now - cnt pos = (k - cnt) % cycle puts array[cnt + pos] % 10 exit end count[x] = now array << x p, q, r = {q, r, p + q + r}.map { |x| x % 10 } end puts r