p, q, r, K = map(int, input().split()) mod_p = p % 10 mod_q = q % 10 mod_r = r % 10 if K == 1: print(mod_p) elif K == 2: print(mod_q) elif K == 3: print(mod_r) else: current_state = (mod_p, mod_q, mod_r) prev_states = {current_state: 3} step = 3 # Last processed step is 3 (A3) found_cycle = False while step < K: step += 1 a, b, c = current_state next_val = (a + b + c) % 10 new_state = (b, c, next_val) if new_state in prev_states: # Cycle detected cycle_start = prev_states[new_state] cycle_length = step - cycle_start # Generate cycle_values starting from new_state cycle_values = [] state_in_cycle = new_state for _ in range(cycle_length): a_cycle, b_cycle, c_cycle = state_in_cycle val = (a_cycle + b_cycle + c_cycle) % 10 cycle_values.append(val) state_in_cycle = (b_cycle, c_cycle, val) # Calculate the position within the cycle start = cycle_start + 1 remaining = K - start remainder = remaining % cycle_length print(cycle_values[remainder]) found_cycle = True break else: prev_states[new_state] = step current_state = new_state if not found_cycle: print(current_state[2])