import java.util.*; 
import java.lang.*; 

public class Main{

	static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	}

	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	}
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main{

	static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	}

	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	}
} Main static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	} gcd {
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	} if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		} y == 0 && z == 0 y == 0 y 0 z == 0 z 0 {
			return x;
		} return x; x if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		} y == 0 y 0 {
			return gcd(x,z,0);
		} return gcd(x,z,0); gcd(x,z,0) gcd x z 0 {
			return gcd(y, x%y,z);
		} return gcd(y, x%y,z); gcd(y, x%y,z) gcd y x%y x y z int x x int y y int z z public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	} main {
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int sum = 0; sum 0 for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(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++){
					sum = sum + gcd(a,b,c);
				}
			}
		} {
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		} for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(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++){
					sum = sum + gcd(a,b,c);
				}
			} {
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			} for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				} int c=1; int c=1; c 1 c<=K c K c++ c++ c {
					sum = sum + gcd(a,b,c);
				} {
					sum = sum + gcd(a,b,c);
				} sum = sum + gcd(a,b,c) sum sum + gcd(a,b,c) sum gcd(a,b,c) gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main{

	static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	}

	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	}
} public class Main{

	static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	}

	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	}
} Main
import java.util.*; 
import java.lang.*; 

public class Main{

	static int gcd(int x, int y, int z){
		if(y == 0 && z == 0){
			return x;
		}
		else if(y == 0){
			return gcd(x,z,0);
		}
		else{
			return gcd(y, x%y,z);
		}
	}

	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);

		int K = scan.nextInt();
		int sum = 0;
		
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					sum = sum + gcd(a,b,c);
				}
			}
		}
		
		System.out.println(sum);
	}
}