import java.util.*;

public class Main {

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

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	}

	public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	}

	public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	}
} import java.util.*; util.* java public class Main {

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

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	}

	public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	}

	public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc sc.close() sc.close sc long ans = 0; ans 0 for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(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++) {
					ans += threeGCD(i, j, k);
				}
			}
		} {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		} for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(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++) {
					ans += threeGCD(i, j, k);
				}
			} {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			} for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				} int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
					ans += threeGCD(i, j, k);
				} {
					ans += threeGCD(i, j, k);
				} ans += threeGCD(i, j, k) ans threeGCD(i, j, k) threeGCD i j k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	} GCD {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	} if (b == 0) {
			return a;
		} b == 0 b 0 {
			return a;
		} return a; a return GCD(b, a % b); GCD(b, a % b) GCD b a % b a b long a a long b b public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	} threeGCD {
		long d = GCD(a, b);
		return GCD(c, d);
	} long d = GCD(a, b); d GCD(a, b) GCD a b return GCD(c, d); GCD(c, d) GCD c d long a a long b b long c c public class Main {

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

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	}

	public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	}

	public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	}
} public class Main {

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

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	}

	public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	}

	public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	}
} Main
import java.util.*;

public class Main {

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

		long ans = 0;

		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				for (int k = 1; k <= K; k++) {
					ans += threeGCD(i, j, k);
				}
			}
		}

		System.out.println(ans);
	}

	public static long GCD(long a, long b) {
		if (b == 0) {
			return a;
		}
		return GCD(b, a % b);
	}

	public static long threeGCD(long a, long b, long c) {
		long d = GCD(a, b);
		return GCD(c, d);
	}
}
