import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	}

	private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	}

	private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	}

	private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	}

	private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	}

} Main public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	} main {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	} Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); in new Scanner(new BufferedReader(new InputStreamReader(System.in))) int n = in.nextInt(); n in.nextInt() in.nextInt in long r = solve(n); r solve(n) solve n System.out.println(r) System.out.println System.out System System.out r in.close() in.close in String[] args args private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	} solve {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	} long r = 0; r 0 for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(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++) {
					r += gcm(i, gcm(j, k));
				} for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				} int j = 1; int j = 1; j 1 j <= n j n j++ j++ j for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				} for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				} int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
					r += gcm(i, gcm(j, k));
				} {
					r += gcm(i, gcm(j, k));
				} r += gcm(i, gcm(j, k)) r gcm(i, gcm(j, k)) gcm i gcm(j, k) gcm j k return r; r int n n private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	} gcm {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	} if (a < b)
			return gcm(b, a); a < b a b return gcm(b, a); gcm(b, a) gcm b a if (a % b == 0)
			return b; a % b == 0 a % b a b 0 return b; b return gcm(b, a % b); gcm(b, a % b) gcm b a % b a b int a a int b b public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	}

	private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	}

	private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	}

} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	}

	private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	}

	private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	}

} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		long r = solve(n);
		System.out.println(r);
		in.close();
	}

	private static long solve(int n) {
		long r = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++) {
					r += gcm(i, gcm(j, k));
				}
		return r;
	}

	private static int gcm(int a, int b) {
		if (a < b)
			return gcm(b, a);
		if (a % b == 0)
			return b;
		return gcm(b, a % b);
	}

}