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 i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	}

	static int gcd(int a,int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return 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 i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	}

	static int gcd(int a,int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	}

} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int ans = 0;

		for(int i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	} main {
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int ans = 0;

		for(int i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	} 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 i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		} int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		} {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		} for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			} int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			} {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			} for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				} int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
					ans += gcd(i,gcd(j,k));
				} {
					ans += gcd(i,gcd(j,k));
				} ans += gcd(i,gcd(j,k)) ans gcd(i,gcd(j,k)) gcd i gcd(j,k) gcd j k System.out.println(ans) System.out.println System.out System System.out ans 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 public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int ans = 0;

		for(int i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	}

	static int gcd(int a,int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return 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 i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	}

	static int gcd(int a,int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return 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 i = 1;i <= K;i++) {
			for(int j = 1;j <= K;j++) {
				for(int k = 1;k <= K;k++) {
					ans += gcd(i,gcd(j,k));
				}
			}
		}

		System.out.println(ans);

	}

	static int gcd(int a,int b) {
		int temp;
		while((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	}

}