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 = Integer.parseInt(a.charAt(a.length()-1)+""); long aa = 0; if(b.length()>1){ aa = Integer.parseInt(b.substring(b.length()-2, b.length())); }else{ aa=Integer.parseInt(b.charAt(b.length()-1)+""); } aa-=1; aa%=4; aa++; System.out.println((int)(Math.pow(c, aa)%10)); } }