#include using namespace std; #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() #define SIZE(x) ll(x.size()) template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long ll; const long long INF = 1LL << 60; const long long MOD = 1000000007; typedef pair P; typedef tuple T; int main() { ll p, q, r, K; cin >> p >> q >> r >> K; p %= 10; q %= 10; r %= 10; K -= 3; map mp; map revmp; mp[{p, q, r}] = 0; revmp[0] = r; int cnt = 1; while (1) { int tr = r, tq = q; r = (p + q + r) % 10; q = tr; p = tq; if (mp.count({p, q, r}) > 0) break; mp[{p, q, r}] = cnt; revmp[cnt] = r; K--; cnt++; if (K == 0) { cout << r << endl; return 0; } } K %= cnt; K--; if (K < 0) K += cnt; cout << revmp[K] << endl; return 0; }