import java.util.*;
import java.io.*;
 
public class Main {
    public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    }
  
  	public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    }
  	
  	public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    }
  
  	public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    }
  	
  	public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    }
} Main public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    } main {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    } final Scanner s = new Scanner(System.in); s new Scanner(System.in) int num = Integer.parseInt(s.next()); num Integer.parseInt(s.next()) Integer.parseInt Integer s.next() s.next s long result = 0; result 0 for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k))); int i = 1; int i = 1; i 1 i <= num i num i++ i++ i for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k))); for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k))); int j = 1; int j = 1; j 1 j <= num j num j++ j++ j for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k))); for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k))); int k = 1; int k = 1; k 1 k <= num k num k++ k++ k result += (long)(gcd(i,gcd(j,k))); result += (long)(gcd(i,gcd(j,k))) result (long)(gcd(i,gcd(j,k))) gcd i gcd(j,k) gcd j k System.out.println(result) System.out.println System.out System System.out result final String[] args args public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    } gcd {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    } if (x < y) swap(x, y); x < y x y swap(x, y) swap x y if (x % y == 0) return y;
      	else return gcd(y, x%y); x % y == 0 x % y x y 0 return y; y return gcd(y, x%y); gcd(y, x%y) gcd y x%y x y int x x int y y public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    } swap {
        int t = x;

        x = y;
        y = t;
    } int t = x; t x x = y x y y = t y t int x x int y y public class Main {
    public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    }
  
  	public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    }
  	
  	public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    }
} public class Main {
    public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    }
  
  	public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    }
  	
  	public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    }
} Main
import java.util.*;
import java.io.*;
 
public class Main {
    public static void main (final String[] args) {
        final Scanner s = new Scanner(System.in);
      	int num = Integer.parseInt(s.next());
      
      	long result = 0;
      	
      	for(int i = 1; i <= num; i++)
          for(int j = 1; j <= num; j++)
            for(int k = 1; k <= num; k++) result += (long)(gcd(i,gcd(j,k)));
      	
      	System.out.println(result);
    }
  
  	public static int gcd(int x, int y) {
      	if (x < y) swap(x, y);
      	if (x % y == 0) return y;
      	else return gcd(y, x%y);
    }
  	
  	public static void swap(int x, int y) {
        int t = x;

        x = y;
        y = t;
    }
}