import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int K = scanner.nextInt();

		int sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		}

		System.out.println(sum);
	}

	public static int gcd(int x, int y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	}

	public static int gcd(int x, int y, int z) {
		return gcd(gcd(x, y), z);
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int K = scanner.nextInt();

		int sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		}

		System.out.println(sum);
	}

	public static int gcd(int x, int y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	}

	public static int gcd(int x, int y, int z) {
		return gcd(gcd(x, y), z);
	}
} Main public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int K = scanner.nextInt();

		int sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		}

		System.out.println(sum);
	} main {
		Scanner scanner = new Scanner(System.in);
		int K = scanner.nextInt();

		int sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		}

		System.out.println(sum);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner int sum = 0; sum 0 for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		} int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		} {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		} for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			} int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			} {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			} for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				} int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
					sum += gcd(i, j, k);
				} {
					sum += gcd(i, j, k);
				} sum += gcd(i, j, k) sum gcd(i, j, k) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int x, int y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	} gcd {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	} if (y == 0)
			return x; y == 0 y 0 return x; x return gcd(y, x % y); gcd(y, x % y) gcd y x % y x y int x x int y y public static int gcd(int x, int y, int z) {
		return gcd(gcd(x, y), z);
	} gcd {
		return gcd(gcd(x, y), z);
	} return gcd(gcd(x, y), z); gcd(gcd(x, y), z) gcd gcd(x, y) gcd x y z int x x int y y int z z
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int K = scanner.nextInt();

		int sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					sum += gcd(i, j, k);
				}
			}
		}

		System.out.println(sum);
	}

	public static int gcd(int x, int y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	}

	public static int gcd(int x, int y, int z) {
		return gcd(gcd(x, y), z);
	}
}