import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); scan.close(); int r = N % 10; int ans = 0; int k; switch(r) { case 1: ans = 1; break; case 2: k = M % 4; if(k == 1) { ans = 2; }else if(k == 2) { ans = 4; }else if(k == 3) { ans = 8; }else { ans = 6; } break; case 3: k = M % 4; if(k == 1) { ans = 3; }else if(k == 2) { ans = 9; }else if(k == 3){ ans = 7; }else { ans = 1; } break; case 4: k = M % 2; if(k == 1) { ans = 4; }else { ans = 6; } break; case 5: ans = 5; break; case 6: ans = 6; break; case 7: k = M % 4; if(k == 1) { ans = 7; }else if(k == 2) { ans = 9; }else if(k == 3){ ans = 3; }else { ans = 1; } break; case 8: k = M % 4; if(k == 1) { ans = 8; }else if(k == 2) { ans = 4; }else if(k == 3){ ans = 2; }else { ans = 6; } break; case 9: k = M % 2; if(k == 1) { ans = 9; }else { ans = 1; } break; } System.out.println(ans); } }