import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	}
       static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	}
       static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	} main {
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long sum = (long) 0; sum (long) 0 for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        } int i=1; int i=1; i 1 i<K+1 i K+1 K 1 i++ i++ i {
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        } {
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        } for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          } int j=1; int j=1; j 1 j<K+1 j K+1 K 1 j++ j++ j {
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          } {
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          } for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            } int l=1; int l=1; l 1 l<K+1 l K+1 K 1 l++ l++ l {
              sum += gcd(gcd(i,j),l);
            } {
              sum += gcd(gcd(i,j),l);
            } sum += gcd(gcd(i,j),l) sum gcd(gcd(i,j),l) gcd gcd(i,j) gcd i j l System.out.println(sum) System.out.println System.out System System.out sum return; String[] args args static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;} gcd {return b>0?gcd(b,a%b):a;} return b>0?gcd(b,a%b):a; b>0?gcd(b,a%b):a b>0 b 0 gcd(b,a%b) gcd b a%b a b a 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 = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	}
       static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	}
       static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int K = sc.nextInt();
        long sum = (long) 0;
      	for(int i=1; i<K+1;  i++){
          for(int j=1; j<K+1;  j++){
            for(int l=1; l<K+1;  l++){
              sum += gcd(gcd(i,j),l);
            }
          }
        }
      
      	System.out.println(sum);
        return;
	}
       static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
}
