import java.util.Scanner;
public class Main {
	//最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	}

} import java.util.Scanner; Scanner java.util public class Main {
	//最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	}

} Main //最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	} gcd {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	} int temp; temp while((temp = a%b)!=0) {
			a = b;
			b = temp;
		} (temp = a%b)!=0 (temp = a%b) temp a%b a b 0 {
			a = b;
			b = temp;
		} a = b a b b = temp b temp return b; b int a a int b b public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	} main {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int k = stdIn.nextInt(); k stdIn.nextInt() stdIn.nextInt stdIn long cnt = 0; cnt 0 for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		} int i = 1; int i = 1; i 1 i <=k i k i++ i++ i {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		} {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		} for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			} int j =1; int j =1; j 1 j <= k j k j++ j++ j {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			} {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			} for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				} int m = 1; int m = 1; m 1 m <= k m k m++ m++ m {
					cnt += gcd(gcd(i,j),m);
				} {
					cnt += gcd(gcd(i,j),m);
				} cnt += gcd(gcd(i,j),m) cnt gcd(gcd(i,j),m) gcd gcd(i,j) gcd i j m System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args public class Main {
	//最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	}

} public class Main {
	//最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	}

} Main
import java.util.Scanner;
public class Main {
	//最大公約数gcd
	static int gcd (int a, int b) {
		int temp;
		while((temp = a%b)!=0) {
			a = b;
			b = temp;
		}
		return b;
	}
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int k = stdIn.nextInt();
		long cnt = 0;
		for(int i = 1; i <=k; i++) {
			for(int j =1; j <= k; j++) {
				for(int m = 1; m <= k; m++) {
					cnt += gcd(gcd(i,j),m);
				}
			}
		}
		
		System.out.println(cnt);
	}

}
