import java.util.Scanner;

public class Main {
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(long i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	}

    public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
	
} import java.util.Scanner; Scanner java.util public class Main {
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(long i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	}

    public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
	
} Main static void log(String s) {
		System.out.println(s);
	} log {
		System.out.println(s);
	} System.out.println(s) System.out.println System.out System System.out s String s s static void log(int i) {
		System.out.println(i);
	} log {
		System.out.println(i);
	} System.out.println(i) System.out.println System.out System System.out i int i i static void log(long i) {
		System.out.println(i);
	} log {
		System.out.println(i);
	} System.out.println(i) System.out.println System.out System System.out i long i i static void log(double d) {
		System.out.printf("%.12f\n",d);
	} log {
		System.out.printf("%.12f\n",d);
	} System.out.printf("%.12f\n",d) System.out.printf System.out System System.out "%.12f\n" d double d d public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	} main {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long total=0; total 0 int gcd1; gcd1 for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		} int i=1; int i=1; i 1 i<=k i k i++ i++ i {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		} {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		} for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			} int j=1; int j=1; j 1 j<=k j k j++ j++ j {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			} {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			} gcd1 = gcd2(i,j) gcd1 gcd2(i,j) gcd2 i j for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				} int l=1 ; int l=1 ; l 1 l<=k l k l++ l++ l {
					total += gcd2(gcd1,l);
				} {
					total += gcd2(gcd1,l);
				} total += gcd2(gcd1,l) total gcd2(gcd1,l) gcd2 gcd1 l sc.close() sc.close sc log( total ) log total String[] args args public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    } gcd2 {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    } int r; r while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        } n > 0 n 0 {
            r = m % n;
            m = n;
            n = r;
        } r = m % n r m % n m n m = n m n n = r n r return m; m int m m int n n public class Main {
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(long i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	}

    public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
	
} public class Main {
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(long i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	}

    public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
	
} Main
import java.util.Scanner;

public class Main {
	static void log(String s) {
		System.out.println(s);
	}

	static void log(int i) {
		System.out.println(i);
	}

	static void log(long i) {
		System.out.println(i);
	}

	static void log(double d) {
		System.out.printf("%.12f\n",d);
	}
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int k = sc.nextInt();

		long total=0;
		int gcd1;
		
		for(int i=1; i<=k; i++) {

			for(int j=1; j<=k; j++) {
				gcd1 = gcd2(i,j);
				for(int l=1 ;l<=k; l++) {
					total += gcd2(gcd1,l);
				}
			}
		}
		
		sc.close();
		
		log( total );
		
	}

    public static int gcd2(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
	
}
