import java.util.*;

public class Main {

	static int gcd[][];

	public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	}

	public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	}

	public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
} import java.util.*; util.* java public class Main {

	static int gcd[][];

	public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	}

	public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	}

	public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
} Main static int gcd[][]; gcd public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	} gcdinit {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	} gcd = new int[n + 1][n + 1] gcd new int[n + 1][n + 1] n + 1 n 1 n + 1 n 1 for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		} int i = 0; int i = 0; i 0 i < n + 1 i n + 1 n 1 ++i ++i i {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		} {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		} for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			} int j = 0; int j = 0; j 0 j < n + 1 j n + 1 n 1 ++j ++j j {
				gcd[i][j] = -1;
			} {
				gcd[i][j] = -1;
			} gcd[i][j] = -1 gcd[i][j] gcd[i] gcd i j -1 1 int n n public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	} gcd {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	} if(gcd[a][b] != -1)return gcd[a][b]; gcd[a][b] != -1 gcd[a][b] gcd[a] gcd a b -1 1 return gcd[a][b]; gcd[a][b] gcd[a] gcd a b if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		} a < b a b {
			int tmp = a;
			a = b;
			b = tmp;
		} int tmp = a; tmp a a = b a b b = tmp b tmp if (b == 0)
			return gcd[a][b] = a; b == 0 b 0 return gcd[a][b] = a; gcd[a][b] = a gcd[a][b] gcd[a] gcd a b a int r = a % b; r a % b a b return gcd[a][b] = gcd(b, r); gcd[a][b] = gcd(b, r) gcd[a][b] gcd[a] gcd a b gcd(b, r) gcd b r int a a int b b public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	} gcd {
		return gcd(gcd(a, b), c);
	} return gcd(gcd(a, b), c); gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c int a a int b b int c c public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc gcdinit(k) gcdinit k sc.close() sc.close sc long ans = 0; ans 0 for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		} int i = 1; int i = 1; i 1 i <= k i k ++i ++i i {
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		} {
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		} for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			} int j = 1; int j = 1; j 1 j <= k j k ++j ++j j {
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			} {
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			} for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				} int l = 1; int l = 1; l 1 l <= k l k ++l ++l l {
					ans += gcd(i, j, l);
				} {
					ans += gcd(i, j, l);
				} ans += gcd(i, j, l) ans gcd(i, j, l) gcd i j l System.out.println(ans) System.out.println System.out System System.out ans String args[] args public class Main {

	static int gcd[][];

	public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	}

	public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	}

	public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
} public class Main {

	static int gcd[][];

	public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	}

	public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	}

	public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
} Main
import java.util.*;

public class Main {

	static int gcd[][];

	public static void gcdinit(int n) {
		gcd = new int[n + 1][n + 1];
		for (int i = 0; i < n + 1; ++i) {
			for (int j = 0; j < n + 1; ++j) {
				gcd[i][j] = -1;
			}
		}
	}

	public static int gcd(int a, int b) {
		if(gcd[a][b] != -1)return gcd[a][b];
		if (a < b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		if (b == 0)
			return gcd[a][b] = a;
		int r = a % b;
		return gcd[a][b] = gcd(b, r);
	}

	public static int gcd(int a, int b, int c) {
		return gcd(gcd(a, b), c);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		gcdinit(k);
		sc.close();
		long ans = 0;
		for(int i = 1; i <= k; ++i){
			for(int j = 1; j <= k; ++j){
				for(int l = 1; l <= k; ++l){
					ans += gcd(i, j, l);
				}
			}
		}
		System.out.println(ans);
	}
}
