
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	}

	static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	}

	static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(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 for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			}
		} int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			}
		} {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			}
		} for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			} {
				for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				}
			} for(int l = 1; l <= k; l++) {
					total += gcd(gcd(i,j),l);
				} int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
					total += gcd(gcd(i,j),l);
				} {
					total += gcd(gcd(i,j),l);
				} total += gcd(gcd(i,j),l) total gcd(gcd(i,j),l) gcd gcd(i,j) gcd i j l System.out.println(total) System.out.println System.out System System.out total String[] args args static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	} gcd {
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	} int tmp; tmp while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		} a % b != 0 a % b a b 0 {
			tmp = b;
			b = a % b;
			a = tmp;
		} tmp = b tmp b b = a % b b a % b a b a = tmp a tmp return b; b int a a int b b public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	}

	static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	}

	static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	}

} Main

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int k = sc.nextInt();
		long total = 0;

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

		System.out.println(total);
	}

	static int gcd(int a, int b){
		int tmp;

		while (a % b != 0)
		{
			tmp = b;
			b = a % b;
			a = tmp;
		}
		return b;
	}

}
