import java.util.*; public class Exercise104{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); String str = sc.next(); int n = Character.getNumericValue(str.charAt(str.length() - 1)); str = sc.next(); if(str.equals("0")){ System.out.println(1); return; } String x = ""; if(str.length() > 1){ x += String.valueOf(str.charAt(str.length() - 2)); } x += String.valueOf(str.charAt(str.length() - 1)); int m = Integer.parseInt(x); int[] choises = new int[4]; for(int i = 1; i <= 4; i++){ choises[i % 4] = (int)Math.pow(n, i) % 10; } System.out.println(choises[m % 4]); } }