import java.util.Scanner;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class Main {	
	
	static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	}
	
	static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	}

} import java.util.Scanner; Scanner java.util import java.util.LinkedList; LinkedList java.util import java.util.PriorityQueue; PriorityQueue java.util import java.util.Queue; Queue java.util public class Main {	
	
	static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	}
	
	static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	}

} Main static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	} GCD {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	} int temp; temp while((temp = A%B) != 0) {
			A = B;
			B = temp;
		} (temp = A%B) != 0 (temp = A%B) temp A%B A B 0 {
			A = B;
			B = temp;
		} A = B A B B = temp B temp return B; B int A A int B B static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	} gcd {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	} int temp; temp int d = GCD(a,b); d GCD(a,b) GCD a b while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		} (temp = c%d) != 0 (temp = c%d) temp c%d c d 0 {
			c = d;
			
			d = temp;
		} c = d c d d = temp d temp return d; d int a a int b b int c c public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		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 int sum = 0; sum 0 for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		} int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		} {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		} for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			} int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			} {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			} for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				} int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
					sum += gcd(i,j,k);
				} {
					sum += gcd(i,j,k);
				} sum += gcd(i,j,k) sum gcd(i,j,k) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {	
	
	static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	}
	
	static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	}

} public class Main {	
	
	static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	}
	
	static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	}

} Main
import java.util.Scanner;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class Main {	
	
	static int GCD(int A, int B) {
		int temp;
		while((temp = A%B) != 0) {
			A = B;
			B = temp;
		}
		return B;
	}
	
	static int gcd(int a, int b, int c) {
		int temp;
		int d = GCD(a,b);
		while((temp = c%d) != 0){
			c = d;
			
			d = temp;
		}
		return d;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		
		int sum = 0;
		
		for(int i = 1; i <= K; i++) {
			for(int j = 1; j <= K; j++) {
				for(int k = 1; k <= K; k++) {
					sum += gcd(i,j,k);
				}
			}
		}
		
		System.out.println(sum);
	}

}