import java.util.Scanner;

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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();
		long sum = 0;
		for(int i = 1; i < k+1; i++) {
			for (int m = 1; m < k+1; m++) {
				for(int l = 1; l <k+1; l++) {
					sum +=yu(yu(i,m),l);
				}
			}
		}
		System.out.println(sum);
		
	}
	static int yu(int a, int b) {
		int c;
		while(true) {
			if(b==0) {
				a =0;
				break;
			}
			c = a%b;
			a = b;
			b = c;
			if(c == 0) {
				break;
			}
			
		
		}
		return a;
	}
	
}
