import java.util.*;

class Main {
	
	private static int mcd(int a, int b, int c){
		return mcd(a, mcd(b, c));
	}
	
	private static int mcd(int a, int b){
		if(a==1 || b==1) return 1; 
		if(a==b) return a;
		if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b);
	}
	
	public static void main(String... args){
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long somma = 0;
		for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c);
		System.out.print(somma);
	}
} import java.util.*; util.* java class Main {
	
	private static int mcd(int a, int b, int c){
		return mcd(a, mcd(b, c));
	}
	
	private static int mcd(int a, int b){
		if(a==1 || b==1) return 1; 
		if(a==b) return a;
		if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b);
	}
	
	public static void main(String... args){
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long somma = 0;
		for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c);
		System.out.print(somma);
	}
} Main private static int mcd(int a, int b, int c){
		return mcd(a, mcd(b, c));
	} mcd {
		return mcd(a, mcd(b, c));
	} return mcd(a, mcd(b, c)); mcd(a, mcd(b, c)) mcd a mcd(b, c) mcd b c int a a int b b int c c private static int mcd(int a, int b){
		if(a==1 || b==1) return 1; 
		if(a==b) return a;
		if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b);
	} mcd {
		if(a==1 || b==1) return 1; 
		if(a==b) return a;
		if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b);
	} if(a==1 || b==1) return 1; a==1 || b==1 a==1 a 1 b==1 b 1 return 1; 1 if(a==b) return a; a==b a b return a; a if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b); a<b a b return mcd(a, b-a); mcd(a, b-a) mcd a b-a b a return mcd(a-b, b); mcd(a-b, b) mcd a-b a b b int a a int b b public static void main(String... args){
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long somma = 0;
		for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c);
		System.out.print(somma);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long somma = 0;
		for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c);
		System.out.print(somma);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long somma = 0; somma 0 for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c); int a=1; int a=1; a 1 a<=K a K ++a ++a a for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c); for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c); int b=1; int b=1; b 1 b<=K b K ++b ++b b for(int c=1; c<=K; ++c) somma+=mcd(a, b, c); for(int c=1; c<=K; ++c) somma+=mcd(a, b, c); int c=1; int c=1; c 1 c<=K c K ++c ++c c somma+=mcd(a, b, c); somma+=mcd(a, b, c) somma mcd(a, b, c) mcd a b c System.out.print(somma) System.out.print System.out System System.out somma String... args args
import java.util.*;

class Main {
	
	private static int mcd(int a, int b, int c){
		return mcd(a, mcd(b, c));
	}
	
	private static int mcd(int a, int b){
		if(a==1 || b==1) return 1; 
		if(a==b) return a;
		if(a<b) return mcd(a, b-a);
		else return mcd(a-b, b);
	}
	
	public static void main(String... args){
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		long somma = 0;
		for(int a=1; a<=K; ++a)
			for(int b=1; b<=K; ++b)
				for(int c=1; c<=K; ++c) somma+=mcd(a, b, c);
		System.out.print(somma);
	}
}