import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				int r = gcd(a, b);
				for (int c = 1; c <= k; c++) {
					sum += gcd(r, c);
				}
			}
		}
		System.out.println(sum);
	}
	
	public static int gcd(int a, int b) {
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (a % b == 0) {
			return b;
		} else {
			return gcd(b, a % b);
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {

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

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

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

public class Main {

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