import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = getNum(sc.next().toCharArray()); int k = getNum(sc.next().toCharArray()); char[] arr = "285714".toCharArray(); int ans = 1; for (int i = 0; i < k; i++) { ans *= n; ans %= 6; } ans = (ans - 1 + 6) % 6; System.out.println(arr[ans]); } static int getNum(char[] arr) { int ans = 0; for (char c : arr) { ans = (ans * 10 + c - '0') % 6; } return ans; } }