import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String s1=sc.next(); String s2=sc.next(); int n=s1.charAt(s1.length()-1)-'0'; int m=0; if(s2.length()<3) m=Integer.valueOf(s2); else m=Integer.valueOf(s2.substring(s2.length()-2,s2.length())); if(m==0) System.out.println(1); else{ m%=4; m+=4; int ans=1; for(int i=1;i<=m;i++) ans*=n; System.out.println(ans%10); } } } }