import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	}

	public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	}

	public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long sum = 0; sum 0 for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		} int a = 1; int a = 1; a 1 a <= k a k ++a ++a a {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		} {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		} for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			} int b = 1; int b = 1; b 1 b <= k b k ++b ++b b {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			} {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			} long g1 = findGcd(a, b); g1 findGcd(a, b) findGcd a b for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				} int c = 1; int c = 1; c 1 c <= k c k ++c ++c c {
					long g2 = findGcd(g1, c);
					sum += g2;
				} {
					long g2 = findGcd(g1, c);
					sum += g2;
				} long g2 = findGcd(g1, c); g2 findGcd(g1, c) findGcd g1 c sum += g2 sum g2 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	} findGcd {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	} if (b == 0L) {
			return a;
		} b == 0L b 0L {
			return a;
		} return a; a return findGcd(b, a % b); findGcd(b, a % b) findGcd b a % b a b long a a long b b public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	}

	public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	}

	public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long sum = 0;
		for (int a = 1; a <= k; ++a) {
			for (int b = 1; b <= k; ++b) {
				long g1 = findGcd(a, b);
				for (int c = 1; c <= k; ++c) {
					long g2 = findGcd(g1, c);
					sum += g2;
				}
			}
		}

		System.out.println(sum);
	}

	public static long findGcd(long a, long b) {
		if (b == 0L) {
			return a;
		}
		return findGcd(b, a % b);
	}

}
