//package atcoder;
 
import java.util.*;
 
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	}
	private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	}
	
} import java.util.*; util.* java public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	}
	private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	}
	
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long sum = 0; sum 0 for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		} int a=1; int a=1; a 1 a<K+1 a K+1 K 1 a++ a++ a {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		} {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		} for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			} int b=1; int b=1; b 1 b<K+1 b K+1 K 1 b++ b++ b {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			} {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			} for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				} int c=1; int c=1; c 1 c<K+1 c K+1 K 1 c++ c++ c {
					sum += gcd(gcd(a,b),c);
				} {
					sum += gcd(gcd(a,b),c);
				} sum += gcd(gcd(a,b),c) sum gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	} gcd {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	} if(m<n) return gcd(n,m); m<n m n return gcd(n,m); gcd(n,m) gcd n m if(n==0) return m; n==0 n 0 return m; m return gcd(n,m%n); gcd(n,m%n) gcd n m%n m n long m m long n n public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	}
	private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	}
	
} public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	}
	private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	}
	
} Main
//package atcoder;
 
import java.util.*;
 
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long sum = 0;
		for(int a=1;a<K+1;a++) {
			for(int b=1;b<K+1;b++) {
				for(int c=1;c<K+1;c++) {
					sum += gcd(gcd(a,b),c);
				}
			}
		}
		System.out.println(sum);
	
	}
	private static long gcd(long m,long n) {
		if(m<n) return gcd(n,m);
		if(n==0) return m;
		return gcd(n,m%n);
	}
	
}