import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p = Integer.parseInt(sc.next()) % 10; int q = Integer.parseInt(sc.next()) % 10; int r = Integer.parseInt(sc.next()) % 10; long K = Long.parseLong(sc.next()); sc.close(); long[] cnt = new long[1000]; long i = 3; long ans = 0; while ( true ) { if ( i == K ) { ans = r; break; } if ( cnt[100 * p + 10 * q + r] > 0 ) { long per = i - cnt[100 * p + 10 * q + r]; long x = (K - i) / per; i += x * per; i++; for ( ; i <= K ; i++ ) { int n = (p + q + r) % 10; p = q; q = r; r = n; } ans = r; break; } else { cnt[100 * p + 10 * q + r] = i++; } int n = (p + q + r) % 10; p = q; q = r; r = n; } System.out.println(ans); } }