import java.util.Scanner;

public class Main {

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

	public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		}

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

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

	public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int result = 0;
		for (int a = 1; a <= K; a++) {
			for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			}
		}
		System.out.println(result);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int result = 0;
		for (int a = 1; a <= K; a++) {
			for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			}
		}
		System.out.println(result);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int result = 0; result 0 for (int a = 1; a <= K; a++) {
			for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			}
		} int a = 1; int a = 1; a 1 a <= K a K a++ a++ a {
			for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			}
		} {
			for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			}
		} for (int b = 1; b <= K; b++) {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			} int b = 1; int b = 1; b 1 b <= K b K b++ b++ b {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			} {
				for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				}
			} for (int c = 1; c <= K; c++) {
					result += gcd(gcd(a, b), c);
				} int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
					result += gcd(gcd(a, b), c);
				} {
					result += gcd(gcd(a, b), c);
				} result += gcd(gcd(a, b), c) result gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c System.out.println(result) System.out.println System.out System System.out result String[] args args public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		} gcd {
		return b > 0 ? gcd(b,a%b) : a;
		} return b > 0 ? gcd(b,a%b) : a; b > 0 ? gcd(b,a%b) : a b > 0 b 0 gcd(b,a%b) gcd b a%b a b a int a a int b b public class Main {

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

	public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		}

} public class Main {

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

	public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		}

} Main
import java.util.Scanner;

public class Main {

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

	public static int gcd(int a, int b){
		return b > 0 ? gcd(b,a%b) : a;
		}

}
