import java.io.*;
import java.util.StringTokenizer;



public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	}
	static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
} import java.io.*; io.* java import java.util.StringTokenizer; StringTokenizer java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	}
	static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	} main {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long 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 gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		} 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 gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		} {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		} for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			} int b=1; int b=1; b 1 b<=k b k b++ b++ b {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			} {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			} for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				} int c=1; int c=1; c 1 c<=k c k c++ c++ c {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				} {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				} int gcdab= gcd(a,b); gcdab gcd(a,b) gcd a b int gcdabc = gcd(gcdab,c); gcdabc gcd(gcdab,c) gcd gcdab c sum+=gcdabc sum gcdabc System.out.println(sum) System.out.println System.out System System.out sum String[] args args static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	} gcd {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	} if (y == 0)
			return x; y == 0 y 0 return x; x return gcd(y, x % y); gcd(y, x % y) gcd y x % y x y int x x int y y static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	} Scanner StringTokenizer st; st BufferedReader br; br public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		} Scanner {
			br = new BufferedReader(new InputStreamReader(system));
		} br = new BufferedReader(new InputStreamReader(system)) br new BufferedReader(new InputStreamReader(system)) InputStream system system public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		} Scanner {
			br = new BufferedReader(new FileReader(file));
		} br = new BufferedReader(new FileReader(file)) br new BufferedReader(new FileReader(file)) String file file public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		} next {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		} while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public String nextLine() throws IOException {
			return br.readLine();
		} nextLine {
			return br.readLine();
		} return br.readLine(); br.readLine() br.readLine br public int nextInt() throws IOException {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		} nextDouble {
			return Double.parseDouble(next());
		} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public char nextChar() throws IOException {
			return next().charAt(0);
		} nextChar {
			return next().charAt(0);
		} return next().charAt(0); next().charAt(0) next().charAt next() next 0 public Long nextLong() throws IOException {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public boolean ready() throws IOException {
			return br.ready();
		} ready {
			return br.ready();
		} return br.ready(); br.ready() br.ready br public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		} waitForInput {
			Thread.sleep(3000);
		} Thread.sleep(3000) Thread.sleep Thread 3000 public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	}
	static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	}
	static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
} Main
import java.io.*;
import java.util.StringTokenizer;



public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int k = sc.nextInt();
		long sum=0;
		for(int a=1; a<=k ; a++) {
			for(int b=1;b<=k;b++) {
				for(int c=1;c<=k;c++) {
					int gcdab= gcd(a,b);
					int gcdabc = gcd(gcdab,c);
					sum+=gcdabc;
					
				}
			}
		}
		System.out.println(sum);
		
	}
	static int gcd(int x, int y) {
		if (y == 0)
			return x;
		return gcd(y, x % y);
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}
