import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String first = br.readLine(); int n = first.charAt(first.length() - 1) - '0'; String second = br.readLine(); int m; if (second.length() == 1) { m = second.charAt(0) - '0'; if (m == 0) { System.out.println(1); return; } } else { m = (second.charAt(second.length() - 2) - '0') * 10 + second.charAt(second.length() - 1) - '0'; } int[][] values = new int[][]{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8}, {3, 9, 7, 1, 3, 9, 7, 1, 3, 9, 7}, {4, 6, 4, 6, 4, 6, 4, 6, 4, 6, 4}, {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, {7, 9, 3, 1, 7, 9, 3, 1, 7, 9, 3}, {8, 4, 2, 6, 8, 4, 2, 6, 8, 4, 2}, {9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9}}; System.out.println(values[n][m % 4 + 3]); } }