import java.util.*;
import java.io.PrintWriter;
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	}
	private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
}
} import java.util.*; util.* java import java.io.PrintWriter; PrintWriter java.io public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	}
	private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
}
} Main public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	} main {
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans=0; ans 0 for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		} int i=1; int i=1; i 1 i<=k i k i++ i++ i {
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		} {
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		} for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			} int j=1; int j=1; j 1 j<=k j k j++ j++ j {
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			} {
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			} for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				} int t=1; int t=1; t 1 t<=k t k t++ t++ t {
					ans+=gcd(gcd(i,j),t);
				} {
					ans+=gcd(gcd(i,j),t);
				} ans+=gcd(gcd(i,j),t) ans gcd(gcd(i,j),t) gcd gcd(i,j) gcd i j t System.out.println(ans) System.out.println System.out System System.out ans String[] args args private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
} gcd {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
} if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		} n!=0 n 0 {
			long tmp=m;
			m=n;
			n=tmp%n;
		} long tmp=m; tmp m m=n m n n=tmp%n n tmp%n tmp n return m; m 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 ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	}
	private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
}
} public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	}
	private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
}
} Main
import java.util.*;
import java.io.PrintWriter;
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		long ans=0;
		for(int i=1; i<=k; i++){
			for(int j=1; j<=k; j++){
				for(int t=1; t<=k; t++){
					ans+=gcd(gcd(i,j),t);
				}
			}
		}
		System.out.println(ans);
	}
	private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
		while(n!=0){
			long tmp=m;
			m=n;
			n=tmp%n;
		}
    return m;
}
}
