import java.util.*;
public class Main {

	public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	}
	
	public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	}

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

	public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	}
	
	public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	}

} Main public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	} gcd {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	} if(a<b) return gcd(b,a); a<b a b return gcd(b,a); gcd(b,a) gcd b a if(b==0) return a; b==0 b 0 return a; a return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b int a a int b b public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	} gcd2 {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	} if(a<c) return gcd2(c,a); a<c a c return gcd2(c,a); gcd2(c,a) gcd2 c a if(c==0) return a; c==0 c 0 return a; a return gcd2(c,a%c); gcd2(c,a%c) gcd2 c a%c a c int a a int c c public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	} main {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	} Scanner cin = new Scanner(System.in); cin new Scanner(System.in) int K = cin.nextInt(); K cin.nextInt() cin.nextInt cin int sum = 0; sum 0 int a = 0; a 0 for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			} int i = 0; int i = 0; i 0 i<K i K i++ i++ i for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			} for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			} int j = 0; int j = 0; j 0 j<K j K j++ j++ j {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			} {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			} a = gcd(i+1,j+1) a gcd(i+1,j+1) gcd i+1 i 1 j+1 j 1 for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				} int n= 0; int n= 0; n 0 n<K n K n++ n++ n {
					sum+=gcd(a,n+1);
				} {
					sum+=gcd(a,n+1);
				} sum+=gcd(a,n+1) sum gcd(a,n+1) gcd a n+1 n 1 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	}
	
	public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	}

} public class Main {

	public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	}
	
	public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	}

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

	public static int gcd(int a,int b) {
		
		if(a<b) return gcd(b,a);
		if(b==0) return a;
		
		return gcd(b,a%b);
	}
	
	public static int gcd2(int a,int c) {
		if(a<c) return gcd2(c,a);
		if(c==0) return a;
		
		return gcd2(c,a%c);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int K = cin.nextInt();
		
		int sum = 0;
		int a = 0;;
		for(int i = 0;i<K;i++)
			for(int j = 0;j<K;j++) {
				a = gcd(i+1,j+1);
				for(int n= 0;n<K;n++) {
					sum+=gcd(a,n+1);
				}
			}
		System.out.println(sum);

	}

}