import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  }
  public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  }
  public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  }
} Main public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  } main {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) long k = Long.parseLong(in.readLine()); k Long.parseLong(in.readLine()) Long.parseLong Long in.readLine() in.readLine in long sum = 0L; sum 0L for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c); 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(gcd(a,b),c); for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c); int b=1; int b=1; b 1 b<=k b k b++ b++ b for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c); for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c); int c=1; int c=1; c 1 c<=k c k c++ c++ c sum += gcd(gcd(a,b),c); sum += gcd(gcd(a,b),c) sum gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  } gcd {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  } if(a>b) {
      long t = a;
      a = b;
      b = t;
    } a>b a b {
      long t = a;
      a = b;
      b = t;
    } long t = a; t a a = b a b b = t b t if(a == 0L)
      return b; a == 0L a 0L 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 IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  }
  public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  }
  public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long k = Long.parseLong(in.readLine());
    long sum = 0L;
    for(int a=1; a<=k; a++)
      for(int b=1; b<=k; b++)
        for(int c=1; c<=k; c++)
          sum += gcd(gcd(a,b),c);
    System.out.println(sum);
  }
  public static long gcd(long a, long b) {
    if(a>b) {
      long t = a;
      a = b;
      b = t;
    }
    if(a == 0L)
      return b;
    return gcd(b%a,a);
  }
}