import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  }

  private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  }

} import java.util.Scanner; Scanner java.util public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  }

  private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  }

} Main public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  } main {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan long sum = 0; sum 0 for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    } 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++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    } {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    } for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      } {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      } for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        } int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        } {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        } int first = gcd(Math.max(i, j), Math.min(i, j)); first gcd(Math.max(i, j), Math.min(i, j)) gcd Math.max(i, j) Math.max Math i j Math.min(i, j) Math.min Math i j int res = gcd(Math.max(first, l), Math.min(first, l)); res gcd(Math.max(first, l), Math.min(first, l)) gcd Math.max(first, l) Math.max Math first l Math.min(first, l) Math.min Math first l sum += res sum res System.out.println(sum) System.out.println System.out System System.out sum String[] args args private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  } gcd {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  } int r = a % b; r a % b a b if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    } r == 0 r 0 {
      return b;
    } return b; b {
      return gcd(b, r);
    } return gcd(b, r); gcd(b, r) gcd b r int a a int b b public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  }

  private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  }

} public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  }

  private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  }

} Main
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int k = scan.nextInt();
    long sum = 0;

    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int l = 1; l <= k; l++) {
          int first = gcd(Math.max(i, j), Math.min(i, j));
          int res = gcd(Math.max(first, l), Math.min(first, l));
          sum += res;
        }
      }
    }

    System.out.println(sum);

  }

  private static int gcd(int a, int b) {
    int r = a % b;
    if (r == 0) {
      return b;
    } else {
      return gcd(b, r);
    }
  }

}