import java.util.Scanner;
class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int gcdTotal = 0;
		for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		}
		System.out.println(gcdTotal);
	}

	private static int getGcd(int a, int b) {
		while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		}
		return b;
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int gcdTotal = 0;
		for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		}
		System.out.println(gcdTotal);
	}

	private static int getGcd(int a, int b) {
		while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		}
		return b;
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int gcdTotal = 0;
		for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		}
		System.out.println(gcdTotal);
	} main {

		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int gcdTotal = 0;
		for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		}
		System.out.println(gcdTotal);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int gcdTotal = 0; gcdTotal 0 for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		} 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++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		} {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		} for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			} int b=1; int b=1; b 1 b<=K b K b++ b++ b {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			} {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			} for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				} int c=1; int c=1; c 1 c<=K c K c++ c++ c {
					gcdTotal += getGcd(c,getGcd(a, b));
				} {
					gcdTotal += getGcd(c,getGcd(a, b));
				} gcdTotal += getGcd(c,getGcd(a, b)) gcdTotal getGcd(c,getGcd(a, b)) getGcd c getGcd(a, b) getGcd a b System.out.println(gcdTotal) System.out.println System.out System System.out gcdTotal String[] args args private static int getGcd(int a, int b) {
		while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		}
		return b;
	} getGcd {
		while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		}
		return b;
	} while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		} true {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		} int d = b; d b if(a%b == 0)break; a%b == 0 a%b a b 0 break; b = a%b b a%b a b a = d a d return b; b int a a int b b
import java.util.Scanner;
class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int K = Integer.parseInt(sc.next());
		int gcdTotal = 0;
		for(int a=1; a<=K; a++) {
			for(int b=1; b<=K; b++) {
				for(int c=1; c<=K; c++) {
					gcdTotal += getGcd(c,getGcd(a, b));
				}
			}
		}
		System.out.println(gcdTotal);
	}

	private static int getGcd(int a, int b) {
		while(true) {
			int d = b;
			if(a%b == 0)break;
			b = a%b;
			a = d;
		}
		return b;
	}
}