import java.util.*;

class Main{
  
  static int gcd(int a, int b){
    if ( a < b )
      gcd(b,a);
    if ( b == 0)
      return a;
    else
      return gcd(b, a % b);
  }
  
  static int gcd_3(int a, int b, int c){
    int X = gcd(a,b);
    return gcd(X,c);
  }
  
  
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int sum = 0;
    
    for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    }
    
    System.out.println(sum);
  }
  
} import java.util.*; util.* java class Main{
  
  static int gcd(int a, int b){
    if ( a < b )
      gcd(b,a);
    if ( b == 0)
      return a;
    else
      return gcd(b, a % b);
  }
  
  static int gcd_3(int a, int b, int c){
    int X = gcd(a,b);
    return gcd(X,c);
  }
  
  
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int sum = 0;
    
    for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    }
    
    System.out.println(sum);
  }
  
} Main static int gcd(int a, int b){
    if ( a < b )
      gcd(b,a);
    if ( b == 0)
      return a;
    else
      return gcd(b, a % b);
  } gcd {
    if ( a < b )
      gcd(b,a);
    if ( b == 0)
      return a;
    else
      return gcd(b, a % b);
  } if ( a < b )
      gcd(b,a); a < b a b gcd(b,a) gcd b a if ( b == 0)
      return a;
    else
      return gcd(b, a % b); b == 0 b 0 return a; a return gcd(b, a % b); gcd(b, a % b) gcd b a % b a b int a a int b b static int gcd_3(int a, int b, int c){
    int X = gcd(a,b);
    return gcd(X,c);
  } gcd_3 {
    int X = gcd(a,b);
    return gcd(X,c);
  } int X = gcd(a,b); X gcd(a,b) gcd a b return gcd(X,c); gcd(X,c) gcd X c int a a int b b int c c public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int sum = 0;
    
    for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    }
    
    System.out.println(sum);
  } main {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int sum = 0;
    
    for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    }
    
    System.out.println(sum);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int sum = 0; sum 0 for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    } {
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    } for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      } {
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      } for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
          sum = sum + gcd_3(i,j,k);
        } {
          sum = sum + gcd_3(i,j,k);
        } sum = sum + gcd_3(i,j,k) sum sum + gcd_3(i,j,k) sum gcd_3(i,j,k) gcd_3 i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.*;

class Main{
  
  static int gcd(int a, int b){
    if ( a < b )
      gcd(b,a);
    if ( b == 0)
      return a;
    else
      return gcd(b, a % b);
  }
  
  static int gcd_3(int a, int b, int c){
    int X = gcd(a,b);
    return gcd(X,c);
  }
  
  
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int sum = 0;
    
    for (int i = 1; i <= K; i++){
      for (int j = 1; j <= K; j++){
        for (int k = 1; k <= K; k++){
          sum = sum + gcd_3(i,j,k);
        }
      }
    }
    
    System.out.println(sum);
  }
  
}