import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char N[] = new char[10002], M[] = new char[10002]; N = sc.next().toCharArray(); M = sc.next().toCharArray(); sc.close(); int N_length = N.length, M_length = M.length, tmp; if (M[0] == '0') System.out.print(1); else if (N[N_length - 1] == '0') System.out.print(0); else { if (N[N_length - 1] == '1' || N[N_length - 1] == '5' || N[N_length - 1] == '6') System.out.print(N[N_length - 1]); else if (N[N_length - 1] == '4' || N[N_length - 1] == '9') { if ((M[M_length - 1] - '0') % 2 == 0) { if (N[N_length - 1] == '4') System.out.print(6); else System.out.print(1); } else System.out.print(N[N_length - 1]); } else { if (M_length == 1) tmp = M[0] - '0'; else tmp = 10 * (M[M_length - 2] - '0') + M[M_length - 1] - '0'; if (N[N_length - 1] == '2') { if (tmp % 4 == 1) System.out.print(2); else if (tmp % 4 == 2) System.out.print(4); else if (tmp % 4 == 3) System.out.print(8); else System.out.print(6); } else if (N[N_length - 1] == '3') { if (tmp % 4 == 1) System.out.print(3); else if (tmp % 4 == 2) System.out.print(9); else if (tmp % 4 == 3) System.out.print(7); else System.out.print(1); } else if (N[N_length - 1] == '7') { if (tmp % 4 == 1) System.out.print(7); else if (tmp % 4 == 2) System.out.print(9); else if (tmp % 4 == 3) System.out.print(3); else System.out.print(1); } else { if (tmp % 4 == 1) System.out.print(8); else if (tmp % 4 == 2) System.out.print(4); else if (tmp % 4 == 3) System.out.print(2); else System.out.print(6); } } } } }