import java.util.Scanner;

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

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	}

	public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	}

} 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();
		sc.close();

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	}

	public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	}

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

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc sc.close() sc.close sc 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++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		} 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++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		} {
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		} for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			} int b=1; int b=1; b 1 b<=K b K b++ b++ b {
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			} {
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			} for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				} int c=1; int c=1; c 1 c<=K c K c++ c++ c {
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				} {
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				} int x = getGCD(getGCD(a, b), c); x getGCD(getGCD(a, b), c) getGCD getGCD(a, b) getGCD a b c sum += x sum x System.out.printf("%d\n", sum) System.out.printf System.out System System.out "%d\n" sum String[] args args public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	} getGCD {
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	} int[] numArray = {a, b}; numArray {a, b} a b int x; x int y; y if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        } numArray[0] > numArray[1] numArray[0] numArray 0 numArray[1] numArray 1 {
            x = numArray[0];
            y = numArray[1];
        } x = numArray[0] x numArray[0] numArray 0 y = numArray[1] y numArray[1] numArray 1 {
            x = numArray[1];
            y = numArray[0];
        } x = numArray[1] x numArray[1] numArray 1 y = numArray[0] y numArray[0] numArray 0 int tmp; tmp while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		} (tmp = x % y) != 0 (tmp = x % y) tmp x % y x y 0 {
            x = y;
            y = tmp;
		} x = y x y y = tmp y tmp return y; y 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();
		sc.close();

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	}

	public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	}

} public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		sc.close();

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	}

	public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	}

} Main
import java.util.Scanner;

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

		int sum = 0;
		for(int a=1; a<=K; a++){
			for(int b=1; b<=K; b++){
				for(int c=1; c<=K; c++){
					int x = getGCD(getGCD(a, b), c);
					//System.out.printf("%d %d %d %d\n", a, b, c, x);
					sum += x;
				}
			}
		}
		
		System.out.printf("%d\n", sum);
	}

	public static int getGCD(int a, int b){
		int[] numArray = {a, b};
		int x;
        int y;
        if (numArray[0] > numArray[1]) {
            x = numArray[0];
            y = numArray[1];
        } else {
            x = numArray[1];
            y = numArray[0];
        }

        // ここからがユークリッドの互除法を用いた最大公約数を
        // 求める為の式
        int tmp;
        while ((tmp = x % y) != 0) {
            x = y;
            y = tmp;
		}

		return y;
	}

}