import java.util.Scanner;

public class Main {

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

} 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();
		sc.close();
		int total = 0;
		for(int a = 1; a <= K; a++) {
			for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		}
		System.out.println(total);
	}
	
	static int gcd(int a, int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	}
	
	static int gcdThree(int a , int b, int c) {
		int gcdTwo = gcd(a, b);
		int result = gcd(gcdTwo , c);
		return result;
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();
		int total = 0;
		for(int a = 1; a <= K; a++) {
			for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		}
		System.out.println(total);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();
		int total = 0;
		for(int a = 1; a <= K; a++) {
			for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		}
		System.out.println(total);
	} 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 int total = 0; total 0 for(int a = 1; a <= K; a++) {
			for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		} 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++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		} {
			for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			}
		} for(int b = 1; b <= K; b++) {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			} int b = 1; int b = 1; b 1 b <= K b K b++ b++ b {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			} {
				for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				}
			} for(int c = 1; c <= K; c++) {
					int result = gcdThree(a , b, c);
					total += result;
				} int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
					int result = gcdThree(a , b, c);
					total += result;
				} {
					int result = gcdThree(a , b, c);
					total += result;
				} int result = gcdThree(a , b, c); result gcdThree(a , b, c) gcdThree a b c total += result total result System.out.println(total) System.out.println System.out System System.out total String[] args args static int gcd(int a, int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	} gcd {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	} int temp; temp while((temp = a % b) != 0) {
			a = b;
			b = temp;
		} (temp = a % b) != 0 (temp = a % b) temp a % b a b 0 {
			a = b;
			b = temp;
		} a = b a b b = temp b temp return b; b int a a int b b static int gcdThree(int a , int b, int c) {
		int gcdTwo = gcd(a, b);
		int result = gcd(gcdTwo , c);
		return result;
	} gcdThree {
		int gcdTwo = gcd(a, b);
		int result = gcd(gcdTwo , c);
		return result;
	} int gcdTwo = gcd(a, b); gcdTwo gcd(a, b) gcd a b int result = gcd(gcdTwo , c); result gcd(gcdTwo , c) gcd gcdTwo c return result; result int a a int b b int c c public class Main {

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

} public class Main {

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

} Main
import java.util.Scanner;

public class Main {

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

}