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

public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  }
  
  static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  }
} import java.util.*; util.* java import java.io.*; io.* java public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  }
  
  static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  }
} Main public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  } main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    } catch(Exception e){
    } Exception e {
    } {
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    } int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br long ans = 0; ans 0 for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(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++){
            ans += gcd(i, gcd(j, k));
          }
        }
      } {
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      } for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        } int j = 1; int j = 1; j 1 j <= N j N j++ j++ j {
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        } {
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        } for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          } int k = 1; int k = 1; k 1 k <= N k N k++ k++ k {
            ans += gcd(i, gcd(j, k));
          } {
            ans += gcd(i, gcd(j, k));
          } ans += gcd(i, gcd(j, k)) ans gcd(i, gcd(j, k)) gcd i gcd(j, k) gcd j k System.out.println(ans) System.out.println System.out System System.out ans String[] args args static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  } gcd {
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  } if(a > b){
      return gcd(b, a);
    } a > b a b {
      return gcd(b, a);
    } return gcd(b, a); gcd(b, a) gcd b a if(a == 0){
      return b;
    } a == 0 a 0 {
      return b;
    } return b; b return gcd(b%a, a); gcd(b%a, a) gcd b%a b a a long a a long b b public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  }
  
  static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  }
} public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  }
  
  static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  }
} Main
import java.util.*;
import java.io.*;

public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      int N = Integer.parseInt(br.readLine());
      long ans = 0;
      for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
          for(int k = 1; k <= N; k++){
            ans += gcd(i, gcd(j, k));
          }
        }
      }
      System.out.println(ans);
    }catch(Exception e){
    }
  }
  
  static long gcd(long a, long b){
    if(a > b){
      return gcd(b, a);
    }
    
    if(a == 0){
      return b;
    }
    
    return gcd(b%a, a);
  }
}