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