import java.util.*;

public class Main {
public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	}
	
	private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        }
} import java.util.*; util.* java public class Main {
public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	}
	
	private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        }
} Main public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	} main {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	} Scanner keyboard = new Scanner(System.in); keyboard new Scanner(System.in) int N = keyboard.nextInt(); N keyboard.nextInt() keyboard.nextInt keyboard int ans = 0; ans 0 for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			} int a=1; int a=1; a 1 a<=N a N a++ a++ a {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			} {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			} for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				} int b=1; int b=1; b 1 b<=N b N b++ b++ b {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				} {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				} int B = gcd(a, b); B gcd(a, b) gcd a b for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					} int c=1; int c=1; c 1 c<=N c N c++ c++ c {
						int C =  gcd(B, c);
						ans += C;
					} {
						int C =  gcd(B, c);
						ans += C;
					} int C =  gcd(B, c); C gcd(B, c) gcd B c ans += C ans C System.out.println(ans) System.out.println System.out System System.out ans keyboard.close() keyboard.close keyboard String[] args args private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        } gcd {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        } if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if(n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n int m m int n n public class Main {
public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	}
	
	private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        }
} public class Main {
public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	}
	
	private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        }
} Main
import java.util.*;

public class Main {
public static void main(String[] args) {

			Scanner keyboard = new Scanner(System.in);

			 int N = keyboard.nextInt();
			 int ans = 0;
			
			for (int a=1; a<=N; a++) {
				for(int b=1; b<=N; b++) {
					int B = gcd(a, b);
					for(int c=1; c<=N; c++) {
						int C =  gcd(B, c);
						ans += C;
					}
				}
			}			
		
		System.out.println(ans);
		keyboard.close();	
	}
	
	private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
        }
}
