import java.util.Scanner;
 
class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		Main m = new Main();
		long sum = 0;
		for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		}
 
		System.out.println(sum);
	}
	int gcd(int a, int b){
		if((a%b!=0)){
			return gcd(b,a%b);
		}
		return b;
	}
	int gcd(int a,int b,int c){
		return gcd(gcd(a,b),c);
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		Main m = new Main();
		long sum = 0;
		for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		}
 
		System.out.println(sum);
	}
	int gcd(int a, int b){
		if((a%b!=0)){
			return gcd(b,a%b);
		}
		return b;
	}
	int gcd(int a,int b,int c){
		return gcd(gcd(a,b),c);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		Main m = new Main();
		long sum = 0;
		for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		}
 
		System.out.println(sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		Main m = new Main();
		long sum = 0;
		for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		}
 
		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 Main m = new Main(); m new Main() long sum = 0; sum 0 for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		} int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		} {
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		} for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			} {
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			} for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				} int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
					sum += m.gcd(i,j,l);
				} {
					sum += m.gcd(i,j,l);
				} sum += m.gcd(i,j,l) sum m.gcd(i,j,l) m.gcd m i j l System.out.println(sum) System.out.println System.out System System.out sum String[] args args int gcd(int a, int b){
		if((a%b!=0)){
			return gcd(b,a%b);
		}
		return b;
	} gcd {
		if((a%b!=0)){
			return gcd(b,a%b);
		}
		return b;
	} if((a%b!=0)){
			return gcd(b,a%b);
		} (a%b!=0) a%b a b 0 {
			return gcd(b,a%b);
		} return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b return b; b int a a int b b int gcd(int a,int b,int c){
		return gcd(gcd(a,b),c);
	} gcd {
		return gcd(gcd(a,b),c);
	} return gcd(gcd(a,b),c); gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c int a a int b b int c c
import java.util.Scanner;
 
class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		Main m = new Main();
		long sum = 0;
		for(int i = 1;i <= k;i++){
			for(int j = 1;j <= k;j++){
				for(int l = 1;l <= k;l++){
					sum += m.gcd(i,j,l);
				}
			}
		}
 
		System.out.println(sum);
	}
	int gcd(int a, int b){
		if((a%b!=0)){
			return gcd(b,a%b);
		}
		return b;
	}
	int gcd(int a,int b,int c){
		return gcd(gcd(a,b),c);
	}
}