import java.io.FileNotFoundException; import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { long t = System.currentTimeMillis(); new Main().run(); System.err.println(System.currentTimeMillis() - t); } void run() { Scanner sc = new Scanner(System.in); BigInteger N=new BigInteger(sc.next()).mod(BigInteger.valueOf(6)); BigInteger K=new BigInteger(sc.next()); N=N.modPow(K, BigInteger.valueOf(6)); int[] ans=new int[] {2,8,5,7,1,4}; System.out.println(ans[N.intValue()-1]); } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }