#include using namespace std; long long p, q, r, K; long long A[1 << 18]; long long used[1009]; int main() { // Step #1. Input cin >> p >> q >> r >> K; A[1] = p % 10LL; A[2] = q % 10LL; A[3] = r % 10LL; used[A[1] * 1 + A[2] * 10 + A[3] * 100] = 3; // Step #2. Solve long long res = K; for (long long i = 4; i <= K; i++) { A[i] = (A[i - 1] + A[i - 2] + A[i - 3]) % 10LL; long long nex = A[i - 2] * 1 + A[i - 1] * 10 + A[i] * 100; if (used[nex] != 0LL) { long long cycle = i - used[nex]; res = (K - used[nex]) % cycle + used[nex]; break; } used[nex] = i; } // Step #3. Output cout << A[res] << endl; return 0; }