def mat_mul(x,y) len_x = x.size len_y = y[0].size num = y.size ret = Array.new(len_x){Array.new(len_y,0)} len_x.times do |i| len_y.times do |j| num.times do |k| ret[i][j] += x[i][k] * y[k][j] ret[i][j] %= 10 end end end ret end def mat_pow(x,n) size = x.size ret = Array.new(size){Array.new(size,0)} size.times do |i| ret[i][i] = 1 end while n > 0 if n&1 == 1 ret = mat_mul(ret,x) end x = mat_mul(x,x) n >>= 1 end ret end p,q,r,k = gets.chomp.split(" ").map(&:to_i) k -= 3 mat = [[1,1,1],[1,0,0],[0,1,0]] mat = mat_pow(mat,k) ans = mat_mul(mat,[[r],[q],[p]]) puts ans[0][0]