import java.util.Scanner;

public class Main {

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

	public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	}

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

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

	public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();
		int s=0;
		for(int i=1; i<=K; i++) {
			for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			}
		}
		System.out.println(s);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();
		int s=0;
		for(int i=1; i<=K; i++) {
			for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			}
		}
		System.out.println(s);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc sc.close() sc.close sc int s=0; s 0 for(int i=1; i<=K; i++) {
			for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			}
		} int i=1; int i=1; i 1 i<=K i K i++ i++ i {
			for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			}
		} {
			for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			}
		} for(int j=1; j<=K; j++) {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			} int j=1; int j=1; j 1 j<=K j K j++ j++ j {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			} {
				for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				}
			} for(int k=1; k<=K; k++) {
					s+=gcd(gcd(i,j),k);
				} int k=1; int k=1; k 1 k<=K k K k++ k++ k {
					s+=gcd(gcd(i,j),k);
				} {
					s+=gcd(gcd(i,j),k);
				} s+=gcd(gcd(i,j),k) s gcd(gcd(i,j),k) gcd gcd(i,j) gcd i j k System.out.println(s) System.out.println System.out System System.out s String[] args args public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	} gcd {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	} int r; r while((r = a%b) != 0) {
			a=b;
			b=r;
		} (r = a%b) != 0 (r = a%b) r a%b a b 0 {
			a=b;
			b=r;
		} a=b a b b=r b r return b; b int a a int b b public class Main {

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

	public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	}

} public class Main {

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

	public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	}

} Main
import java.util.Scanner;

public class Main {

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

	public static int gcd(int a, int b) {
		int r;
		while((r = a%b) != 0) {
			a=b;
			b=r;
		}
		return b;
	}

}
