#include using namespace std; int main() { int p, q, r; long long K; cin >> p >> q >> r >> K; K--; p %= 10, q %= 10, r %= 10; map, int> seen; int i = 0; while (true) { if (!seen.count({p, q, r})) { seen[{p, q, r}] = i; int tmp = (p + q + r) % 10; p = q, q = r, r = tmp; i++; } else { break; } } int cycle_s = seen[{p, q, r}], cycle_e = i; int cycle_size = cycle_e - cycle_s; vector cycle(cycle_e - cycle_s); for (auto [tup, order] : seen) { if (cycle_s <= order && order < cycle_e) { cycle[order - cycle_s] = get<0>(tup); } } K %= cycle_size; cout << cycle[K] << endl; return 0; }