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