import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	}


	private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	}
	private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	}


	private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	}
	private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	}
} Main public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	} main {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	} int n; n try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		} {
			n = sc.nextInt();
		} Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) n = sc.nextInt() n sc.nextInt() sc.nextInt sc long sum = 0; sum 0 for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		} {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		} for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			} int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			} {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			} for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				} int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
					sum += gcd(i, j, k);
				} {
					sum += gcd(i, j, k);
				} sum += gcd(i, j, k) sum gcd(i, j, k) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	} gcd {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	} while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		} true {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		} int gcd = gcd(b, c); gcd gcd(b, c) gcd b c int tmp = a % gcd; tmp a % gcd a gcd if(tmp == 0) {
				return gcd;
			} tmp == 0 tmp 0 {
				return gcd;
			} return gcd; gcd a = b a b b = c b c c = tmp c tmp int a a int b b int c c private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	} gcd {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	} int tmp; tmp while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        } (tmp = a % b) != 0 (tmp = a % b) tmp a % b a b 0 {
            a = b;
            b = tmp;
        } a = b a b b = tmp b tmp return b; b int a a int b b public class Main {

	public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	}


	private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	}
	private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	}
} public class Main {

	public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	}


	private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	}
	private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int n;
		try(Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
		}
		long sum = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				for(int k = 1; k <= n; k++) {
					sum += gcd(i, j, k);
				}
			}
		}
		System.out.println(sum);
	}


	private static int gcd(int a, int b, int c) {
		while(true) {
			int gcd = gcd(b, c);
			int tmp = a % gcd;
			if(tmp == 0) {
				return gcd;
			}
			
			a = b;
			b = c;
			c = tmp;
		}
	}
	private static int gcd(int a, int b) {
		int tmp;
        while ((tmp = a % b) != 0) {
            a = b;
            b = tmp;
        }
        return b;
	}
}
