import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }

}
 import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long ans = 0; ans 0 for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		} int i=1; int i=1; i 1 i<N+1 i N+1 N 1 i++ i++ i {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		} {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		} for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			} int j=1; int j=1; j 1 j<N+1 j N+1 N 1 j++ j++ j {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			} {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			} for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				} int k=1; int k=1; k 1 k<N+1 k N+1 N 1 k++ k++ k {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				} {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				} int gcd = gcd(i,j); gcd gcd(i,j) gcd i j gcd = gcd(gcd,k) gcd gcd(gcd,k) gcd gcd k ans += gcd ans gcd System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    } gcd {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    } if(num2==0) return num1;
        else return gcd(num2,num1%num2); num2==0 num2 0 return num1; num1 return gcd(num2,num1%num2); gcd(num2,num1%num2) gcd num2 num1%num2 num1 num2 int num1 num1 int num2 num2 public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }

} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long ans = 0;
		for(int i=1;i<N+1;i++) {
			for(int j=1;j<N+1;j++) {
				for(int k=1;k<N+1;k++) {
					int gcd = gcd(i,j);
					gcd = gcd(gcd,k);
					ans += gcd;
//					System.out.println(i);
//					System.out.println(j);
//					System.out.println(k);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }

}

