import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String a=sc.next(); String b = sc.next(); long c = Long.parseLong(a.charAt(a.length()-1)+""); long aa = 0; if(b.length()>1){ aa = Long.parseLong(b.substring(b.length()-2, b.length())); if(aa==0)aa=100; }else { aa=Long.parseLong(b.charAt(b.length()-1)+""); } aa-=1; aa%=4; aa++; System.out.println((int)(Math.pow(c, aa)%10)); } }