#include using namespace std; const int mx = 1000 + 5; int main() { int p, q, r; long long k; cin >> p >> q >> r >> k; k--; p %= 10, q %= 10, r %= 10; vector a(mx, 0); a[0] = p, a[1] = q, a[2] = r; vector used(mx, 0); long long ans = k; used[100 * r + 10 * q + 1 * p] = 2; for (long long i = 3; i <= k; i++) { long long nx = (a[i - 1] + a[i - 2] + a[i - 3]) % 10; long long S = nx * 100 + a[i - 1] * 10 + a[i - 2] * 1; a[i] = nx; if (used[S] != 0) { long long cycle = i - used[S]; ans = (k - used[S]) % cycle + used[S]; break; } used[S] = i; } cout << a[ans] << endl; }