import java.util.Scanner;
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int k;
    k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    }
    System.out.println(sum);
  }

  public static int gcd(int x, int y){ //最大公約数
    if(x<y){
      return gcd(y,x);
    }
    if(y == 0){
      return x;
    }
    return gcd(y,x%y);
  }
} import java.util.Scanner; Scanner java.util class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int k;
    k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    }
    System.out.println(sum);
  }

  public static int gcd(int x, int y){ //最大公約数
    if(x<y){
      return gcd(y,x);
    }
    if(y == 0){
      return x;
    }
    return gcd(y,x%y);
  }
} Main public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int k;
    k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    }
    System.out.println(sum);
  } main {
    Scanner sc = new Scanner(System.in);
    int k;
    k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    }
    System.out.println(sum);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k; k k = sc.nextInt() k sc.nextInt() sc.nextInt sc long sum = 0; sum 0 for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    } int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    } {
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    } for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      } int b = 1; int b = 1; b 1 b <=k b k b++ b++ b {
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      } {
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      } for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        } int c = 1; int c = 1; c 1 c<=k c k c++ c++ c {
          sum += gcd(c,gcd(a,b));
        } {
          sum += gcd(c,gcd(a,b));
        } sum += gcd(c,gcd(a,b)) sum gcd(c,gcd(a,b)) gcd c gcd(a,b) gcd a b System.out.println(sum) System.out.println System.out System System.out sum String args[] args public static int gcd(int x, int y){ //最大公約数
    if(x<y){
      return gcd(y,x);
    }
    if(y == 0){
      return x;
    }
    return gcd(y,x%y);
  } gcd { //最大公約数
    if(x<y){
      return gcd(y,x);
    }
    if(y == 0){
      return x;
    }
    return gcd(y,x%y);
  } if(x<y){
      return gcd(y,x);
    } x<y x y {
      return gcd(y,x);
    } return gcd(y,x); gcd(y,x) gcd y x if(y == 0){
      return x;
    } y == 0 y 0 {
      return x;
    } return x; x return gcd(y,x%y); gcd(y,x%y) gcd y x%y x y int x x int y y
import java.util.Scanner;
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int k;
    k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k ; a++){
      for(int b = 1; b <=k; b++){
        for(int c = 1; c<=k; c++){
          sum += gcd(c,gcd(a,b));
        }
      }
    }
    System.out.println(sum);
  }

  public static int gcd(int x, int y){ //最大公約数
    if(x<y){
      return gcd(y,x);
    }
    if(y == 0){
      return x;
    }
    return gcd(y,x%y);
  }
}
