import java.util.Scanner;

public class Main {

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

	private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	}

	private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(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();
		int ans = 0;
		for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		}
		System.out.println(ans);
		scan.close();
	}

	private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	}

	private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(b , a % b);
	}

} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int ans = 0;
		for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		}
		System.out.println(ans);
		scan.close();
	} main {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int ans = 0;
		for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		}
		System.out.println(ans);
		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int ans = 0; ans 0 for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += 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++) {
					ans += gcd(a,b,c);
				}
			}
		} {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		} for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += 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++) {
					ans += gcd(a,b,c);
				}
			} {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			} for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				} int c = 1 ; int c = 1 ; c 1 c <= k c k c++ c++ c {
					ans += gcd(a,b,c);
				} {
					ans += gcd(a,b,c);
				} ans += gcd(a,b,c) ans gcd(a,b,c) gcd a b c System.out.println(ans) System.out.println System.out System System.out ans scan.close() scan.close scan String[] args args private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	} gcd {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	} int d = gcd2(a,b); d gcd2(a,b) gcd2 a b return gcd2(c,d); gcd2(c,d) gcd2 c d int a a int b b int c c private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(b , a % b);
	} gcd2 {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(b , a % b);
	} if(b == 0) return a; b == 0 b 0 return a; a return a % b == 0 ? b : gcd2(b , a % b); a % b == 0 ? b : gcd2(b , a % b) a % b == 0 a % b a b 0 b gcd2(b , a % b) gcd2 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();
		int ans = 0;
		for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		}
		System.out.println(ans);
		scan.close();
	}

	private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	}

	private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(b , a % b);
	}

} public class Main {

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

	private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	}

	private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(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();
		int ans = 0;
		for(int a = 1 ; a <= k ; a++) {
			for(int b = 1 ; b <= k ; b++) {
				for(int c = 1 ; c <= k ; c++) {
					ans += gcd(a,b,c);
				}
			}
		}
		System.out.println(ans);
		scan.close();
	}

	private static int gcd(int a, int b, int c) {
		int d = gcd2(a,b);
		
		return gcd2(c,d);
		
	}

	private static int gcd2(int a, int b) {
		if(b == 0) return a;
		
		return a % b == 0 ? b : gcd2(b , a % b);
	}

}
