import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	}

	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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	}

	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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int sum = 0; sum 0 for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		} {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		} for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			} int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			} {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			} for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				} int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
					sum += gcd(i,gcd(j, k));
				} {
					sum += gcd(i,gcd(j, k));
				} sum += gcd(i,gcd(j, k)) sum gcd(i,gcd(j, k)) gcd i gcd(j, k) gcd j k System.out.println(sum) System.out.println System.out System System.out sum 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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	}

	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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	}

	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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i,gcd(j, k));
				}
			}
		}
		System.out.println(sum);

	}

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

}