
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int K;
	long sum = 0L;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	}

	void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	}

	void showResult() {
		System.out.println(sum);
	}

	int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	}
} import java.util.*; util.* java import java.util.stream.Collectors; Collectors java.util.stream import java.io.*; io.* java import java.nio.charset.StandardCharsets; StandardCharsets java.nio.charset public class Main {
	int K;
	long sum = 0L;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	}

	void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	}

	void showResult() {
		System.out.println(sum);
	}

	int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	}
} Main int K; K long sum = 0L; sum 0L public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} main {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); reader new InputStreamReader(System.in, StandardCharsets.UTF_8) BufferedReader in = new BufferedReader(reader); in new BufferedReader(reader) Main ins = new Main(in); ins new Main(in) ins.calc() ins.calc ins ins.showResult() ins.showResult ins String[] args args Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	} Main {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	} String[] tokens = in.readLine().split(" "); tokens in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " K = Integer.parseInt(tokens[0]) K Integer.parseInt(tokens[0]) Integer.parseInt Integer tokens[0] tokens 0 BufferedReader in in void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	} calc {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	} for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		} 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) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		} {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		} for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			} int b = 1; int b = 1; b 1 b <= K b K ++b ++b b {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			} {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			} for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				} int c = 1; int c = 1; c 1 c <= K c K ++c ++c c {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				} {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				} int temp = gcd(a, b); temp gcd(a, b) gcd a b int gcd = gcd(c, temp); gcd gcd(c, temp) gcd c temp sum += gcd sum gcd void showResult() {
		System.out.println(sum);
	} showResult {
		System.out.println(sum);
	} System.out.println(sum) System.out.println System.out System System.out sum int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	} gcd {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	} if (large < small) {
			return gcd(small, large);
		} large < small large small {
			return gcd(small, large);
		} return gcd(small, large); gcd(small, large) gcd small large if (large % small == 0) {
			return small;
		} large % small == 0 large % small large small 0 {
			return small;
		} return small; small return gcd(small, large % small); gcd(small, large % small) gcd small large % small large small int large large int small small public class Main {
	int K;
	long sum = 0L;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	}

	void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	}

	void showResult() {
		System.out.println(sum);
	}

	int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	}
} public class Main {
	int K;
	long sum = 0L;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	}

	void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	}

	void showResult() {
		System.out.println(sum);
	}

	int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	}
} Main

import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int K;
	long sum = 0L;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		K = Integer.parseInt(tokens[0]);
	}

	void calc() {
		for (int a = 1; a <= K; ++a) {
			for (int b = 1; b <= K; ++b) {
				for (int c = 1; c <= K; ++c) {
					int temp = gcd(a, b);
					int gcd = gcd(c, temp);
					sum += gcd;
				}
			}
		}
	}

	void showResult() {
		System.out.println(sum);
	}

	int gcd(int large, int small) {
		if (large < small) {
			return gcd(small, large);
		}
		if (large % small == 0) {
			return small;
		}
		return gcd(small, large % small);
	}
}
