import java.util.*;

class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		}
		
		System.out.println(sum);
	}
		
	public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
	}
} import java.util.*; util.* java class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		}
		
		System.out.println(sum);
	}
		
	public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
	}
} Main static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		}
		
		System.out.println(sum);
	} main {
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		}
		
		System.out.println(sum);
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int sum=0; sum 0 for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		} int i=1; int i=1; i 1 i<=n i n i++ i++ i {
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		} {
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		} for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			} int j=1; int j=1; j 1 j<=n j n j++ j++ j {
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			} {
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			} for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				} int l=1; int l=1; l 1 l<=n l n l++ l++ l {
					sum+=gcd(i,gcd(j,l));
				} {
					sum+=gcd(i,gcd(j,l));
				} sum+=gcd(i,gcd(j,l)) sum gcd(i,gcd(j,l)) gcd i gcd(j,l) gcd j l System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
	} gcd {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
	} int r; r while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        } n > 0 n 0 {
            r = m % n;
            m = n;
            n = r;
        } r = m % n r m % n m n m = n m n n = r n r return m; m int m m int n n
import java.util.*;

class Main{
	static public void main(String[] args){
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int sum=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int l=1;l<=n;l++){
					sum+=gcd(i,gcd(j,l));
				}
			}
		}
		
		System.out.println(sum);
	}
		
	public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
	}
}