import java.util.Scanner;


public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	}

	private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	}

} 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();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	}

	private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	}

} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	} main {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans = 0; ans 0 for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		} int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		} {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		} for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			} {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			} for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				} int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				} {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				} int temp = getGcd(i, j); temp getGcd(i, j) getGcd i j ans += getGcd(a, temp) ans getGcd(a, temp) getGcd a temp System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String[] args args private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	} getGcd {
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	} while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		} q != 0 q 0 {
			int r = p % q;
			p = q;
			q = r;
		} int r = p % q; r p % q p q p = q p q q = r q r return p; p int p p int q q public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	}

	private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	}

} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	}

	private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	}

} Main
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		long ans = 0;

		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int a = 1; a <= k; a++) {
					int temp = getGcd(i, j);
					ans += getGcd(a, temp);
				}
			}
		}

		System.out.println(ans);
		sc.close();
	}

	private static int getGcd(int p, int q){
		while(q != 0){
			int r = p % q;
			p = q;
			q = r;
		}
		return p;
	}

}