import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	}

	public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	}

	public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	}
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	}

	public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	}

	public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	} main {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int num = Integer.parseInt(sc.next()); num Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int total = 0; total 0 for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		} int i = 1; int i = 1; i 1 i <= num i num i++ i++ i {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		} {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		} for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			} int j = 1; int j = 1; j 1 j <= num j num j++ j++ j {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			} {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			} for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				} int k = 1; int k = 1; k 1 k <= num k num k++ k++ k {
					total += calc3GCD(i, j, k);
				} {
					total += calc3GCD(i, j, k);
				} total += calc3GCD(i, j, k) total calc3GCD(i, j, k) calc3GCD i j k System.out.println(total) System.out.println System.out System System.out total String[] args args public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	} calc3GCD {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	} int tempGCD = calcGCD(a, b); tempGCD calcGCD(a, b) calcGCD a b return calcGCD(tempGCD, c); calcGCD(tempGCD, c) calcGCD tempGCD c int a a int b b int c c public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	} calcGCD {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	} int[] numArray = { a, b }; numArray { a, b } a b Arrays.sort(numArray) Arrays.sort Arrays numArray int x; x int y; y x = numArray[1] x numArray[1] numArray 1 y = numArray[0] y numArray[0] numArray 0 int tmp; tmp while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		} (tmp = x % y) != 0 (tmp = x % y) tmp x % y x y 0 {
			x = y;
			y = tmp;
		} x = y x y y = tmp y tmp return y; y int a a int b b public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	}

	public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	}

	public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	}
} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	}

	public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	}

	public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	}
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.next());
		int total = 0;
		for (int i = 1; i <= num; i++) {
			for (int j = 1; j <= num; j++) {
				for (int k = 1; k <= num; k++) {
					total += calc3GCD(i, j, k);
				}
			}
		}
		System.out.println(total);
	}

	public static int calc3GCD(int a, int b, int c) {
		int tempGCD = calcGCD(a, b);

		return calcGCD(tempGCD, c);
	}

	public static int calcGCD(int a, int b) {
		int[] numArray = { a, b };
		Arrays.sort(numArray);
		int x;
		int y;
		x = numArray[1];
		y = numArray[0];

		int tmp;
		while ((tmp = x % y) != 0) {
			x = y;
			y = tmp;
		}
		return y;
	}
}
