#include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // #include // using namespace atcoder; // using mint = modint1000000007; // using mint = modint998244353; // const int MOD = 1000000007; // const int MOD = 998244353; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int p, q, r; ll K; cin >> p >> q >> r >> K; K--; vector v(2000, 0); v[0] = p % 10; v[1] = q % 10; v[2] = r % 10; for (int i = 3; i < 2000; i++) { v[i] = (v[i - 1] + v[i - 2] + v[i - 3]) % 10; } ll x, y; bool fg = 0; for (int i = 3; i < 2000; i++) { for (int j = i + 1; j < 2000; j++) { if (v[i - 1] == v[j - 1] && v[i - 2] == v[j - 2] && v[i - 3] == v[j - 3]) { x = i; y = j - i; fg = 1; break; } } if (fg) { break; } } cout << v[x + (K - x + 1) % y - 1] << endl; return 0; }