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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	}
	
	// ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	}
} 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	}
	
	// ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	}
} 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		int K = sc.nextInt();
		
		// 結果の出力
		long sum = 0;
		for (int i = 1; i <= K; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	} 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		} int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		} {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		} for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			} int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			} {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			} int r = euclid(i, j); r euclid(i, j) euclid i j for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				} int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
					sum += euclid(r, k);
				} {
					sum += euclid(r, k);
				} sum += euclid(r, k) sum euclid(r, k) euclid r k System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args // ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	} euclid {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	} if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		} a < b a b {
			int temp = a;
			a = b;
			b = temp;
		} int temp = a; temp a a = b a b b = temp b temp int r = a % b; r a % b a b if (r == 0) {
			return b;
		} r == 0 r 0 {
			return b;
		} return b; b return euclid(b, r); euclid(b, r) euclid b r 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	}
	
	// ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	}
} 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	}
	
	// ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	}
} 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; i++) {
			for (int j = 1; j <= K; j++) {
				int r = euclid(i, j);
				for (int k = 1; k <= K; k++) {
					sum += euclid(r, k);
				}
			}
		}
		System.out.println(sum);

		sc.close();
	}
	
	// ユークリッドの互除法で最大公約数を求める
	static int euclid( int a, int b ) {
		//引数の大小入替
		if (a < b) {
			int temp = a;
			a = b;
			b = temp;
		}
		
		int r = a % b;
		if (r == 0) {
			return b;
		}
		return euclid(b, r);
	}
}
