import java.util.Scanner;

public class Main {

	public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	}

	public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	}

	public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	}
} Main public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	} gcd2 {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	} if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		} a % b == 0 a % b a b 0 {
			return b;
		} return b; b {
			return gcd2(b, a % b);
		} return gcd2(b, a % b); gcd2(b, a % b) gcd2 b a % b a b int a a int b b public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	} gcd {
		return gcd2(a, gcd2(b, c));
	} return gcd2(a, gcd2(b, c)); gcd2(a, gcd2(b, c)) gcd2 a gcd2(b, c) gcd2 b c int a a int b b int c c public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int a a b b c; c long sum = 0; sum 0 for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		} a = 1; a = 1 a 1 a <= K a K a++ a++ a {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		} {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		} for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			} b = 1; b = 1 b 1 b <= K b K b++ b++ b {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			} {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			} for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				} c = 1; c = 1 c 1 c <= K c K c++ c++ c {
					sum += gcd(a, b, c);
				} {
					sum += gcd(a, b, c);
				} sum += gcd(a, b, c) sum gcd(a, b, c) gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	}

	public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	}
} public class Main {

	public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	}

	public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	}
} Main
import java.util.Scanner;

public class Main {

	public static int gcd2(int a, int b) {
		if (a % b == 0) {
			return b;
		} else {
			return gcd2(b, a % b);
		}
	}

	public static int gcd(int a, int b, int c) {
		return gcd2(a, gcd2(b, c));
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a, b, c;
		long sum = 0;
		for (a = 1; a <= K; a++) {
			for (b = 1; b <= K; b++) {
				for (c = 1; c <= K; c++) {
					sum += gcd(a, b, c);
				}
			}
		}

		System.out.println(sum);
	}
}
