import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  }

  static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  }

  static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  }
} Main public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  } main {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner long sum = 0; sum 0 for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    } int a = 1; int a = 1; a 1 a <= K a K a++ a++ a {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    } {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    } for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      } int b = 1; int b = 1; b 1 b <= K b K b++ b++ b {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      } {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      } long d = gcd(a, b); d gcd(a, b) gcd a b for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
          sum += gcd(d, c);
        } {
          sum += gcd(d, c);
        } sum += gcd(d, c) sum gcd(d, c) gcd d c System.out.println(sum) System.out.println System.out System System.out sum String[] args args static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  } gcd {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  } if (i > j) return gcd(j, i); i > j i j return gcd(j, i); gcd(j, i) gcd j i while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    } i > 0 i 0 {
      long r = j % i;
      j = i;
      i = r;
    } long r = j % i; r j % i j i j = i j i i = r i r return j; j long i i long j j public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  }

  static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  }
} public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  }

  static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  }
} Main
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int K = scanner.nextInt();
    long sum = 0;
    for (int a = 1; a <= K; a++) {
      for (int b = 1; b <= K; b++) {
        long d = gcd(a, b);
        for (int c = 1; c <= K; c++) {
          sum += gcd(d, c);
        }
      }
    }
    System.out.println(sum);
  }

  static long gcd(long i, long j) {
    if (i > j) return gcd(j, i);
    while (i > 0) {
      long r = j % i;
      j = i;
      i = r;
    }
    return j;
  }
}
