import java.util.Scanner;

public class Main {

	private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	}

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

	private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	}

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

	private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	}

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

	private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	}

} Main private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	} sumOfgcdTuples {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	} long sum = 0; sum 0 for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		} int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		} {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		} for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			} int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			} {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			} for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c); int c = 1; int c = 1; c 1 c <= k c k c++ c++ c sum += gcd(a, b, c); sum += gcd(a, b, c) sum gcd(a, b, c) gcd a b c return sum; sum int k k private static int gcd(int a, int b, int c) {
		return gcd(a, gcd(b, c));
	} gcd {
		return gcd(a, gcd(b, c));
	} return gcd(a, gcd(b, c)); gcd(a, gcd(b, c)) gcd a gcd(b, c) gcd b c int a a int b b int c c private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	} gcd {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	} if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b); a == 1 || b == 1 a == 1 a 1 b == 1 b 1 return 1; 1 if (a % b == 0)
			return b;
		else
			return gcd(b, a % b); a % b == 0 a % b a b 0 return b; b return gcd(b, a % b); gcd(b, a % b) gcd b a % b a b int a a int b b public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	} main {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	} try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		} {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		} Scanner scanner = new Scanner(System.in) Scanner scanner = new Scanner(System.in) new Scanner(System.in) System.out.println(sumOfgcdTuples(scanner.nextInt())) System.out.println System.out System System.out sumOfgcdTuples(scanner.nextInt()) sumOfgcdTuples scanner.nextInt() scanner.nextInt scanner String[] args args public class Main {

	private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	}

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

	private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	}

} public class Main {

	private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	}

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

	private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	}

} Main
import java.util.Scanner;

public class Main {

	private static long sumOfgcdTuples(int k) {
		long sum = 0;
		for (int a = 1; a <= k; a++) {
			for (int b = 1; b <= k; b++) {
				for (int c = 1; c <= k; c++)
					sum += gcd(a, b, c);
			}
		}
		return sum;
	}

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

	private static int gcd(int a, int b) {
		if (a == 1 || b == 1)
			return 1;
		else if (a % b == 0)
			return b;
		else
			return gcd(b, a % b);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println(sumOfgcdTuples(scanner.nextInt()));
		}
	}

}
