import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	}

	public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	}

} import java.util.Scanner; Scanner java.util public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	}

	public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	} main {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	} Scanner scn = new Scanner(System.in); scn new Scanner(System.in) int N = scn.nextInt(); N scn.nextInt() scn.nextInt scn int sum = 0; sum 0 for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		} {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		} for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			} int j = 1; int j = 1; j 1 j <= N j N j++ j++ j {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			} {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			} for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				} int k = 1; int k = 1; k 1 k <= N k N k++ k++ k {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				} {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				} int c = HCF(i, j); c HCF(i, j) HCF i j int d = HCF(c, k); d HCF(c, k) HCF c k sum = sum + d sum sum + d sum d System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	} HCF {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	} int temp = -1; temp -1 1 int x = Math.max(a, b); x Math.max(a, b) Math.max Math a b int y = Math.min(a, b); y Math.min(a, b) Math.min Math a b while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		} x > 0 x 0 {
			temp = x;
			x = y % x;
			y = temp;
		} temp = x temp x x = y % x x y % x y x y = temp y temp return y; y int a a int b b public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	}

	public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	}

} public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	}

	public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	}

} Main
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int k = 1; k <= N; k++) {
					int c = HCF(i, j);
					int d = HCF(c, k);
					sum = sum + d;
				}
			}
		}
		System.out.println(sum);

	}

	public static int HCF(int a, int b) {
		int temp = -1;
		int x = Math.max(a, b);
		int y = Math.min(a, b);
		while (x > 0) {
			temp = x;
			x = y % x;
			y = temp;
		}
		return y;
	}

}
