import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long ans = 0;

    for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    }
    System.out.println(ans);
  }

  private static int gcd(int i, int j) {
    if(i < j) return gcd(j, i);
    if(j == 0) return i;
    return gcd(j, i % j);
  }
} import java.util.*; util.* java class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long ans = 0;

    for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    }
    System.out.println(ans);
  }

  private static int gcd(int i, int j) {
    if(i < j) return gcd(j, i);
    if(j == 0) return i;
    return gcd(j, i % j);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long ans = 0;

    for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    }
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long ans = 0;

    for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    }
    System.out.println(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long ans = 0; ans 0 for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    } int a = 1; int a = 1; a 1 a <= N a N a++ a++ a {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    } {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    } for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      } int b = 1; int b = 1; b 1 b <= N b N b++ b++ b {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      } {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      } for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        } int c = 1; int c = 1; c 1 c <= N c N c++ c++ c {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        } {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        } gcd(a, b) gcd a b gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c ans = ans + gcd(gcd(a, b), c) ans ans + gcd(gcd(a, b), c) ans gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c System.out.println(ans) System.out.println System.out System System.out ans String[] args args private static int gcd(int i, int j) {
    if(i < j) return gcd(j, i);
    if(j == 0) return i;
    return gcd(j, i % j);
  } gcd {
    if(i < j) return gcd(j, i);
    if(j == 0) return i;
    return gcd(j, i % j);
  } if(i < j) return gcd(j, i); i < j i j return gcd(j, i); gcd(j, i) gcd j i if(j == 0) return i; j == 0 j 0 return i; i return gcd(j, i % j); gcd(j, i % j) gcd j i % j i j int i i int j j
import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long ans = 0;

    for (int a = 1; a <= N; a++) {
      for (int b = 1; b <= N; b++) {
        for (int c = 1; c <= N; c++) {
          gcd(a, b);
          gcd(gcd(a, b), c);
          ans = ans + gcd(gcd(a, b), c);
        }
      }
    }
    System.out.println(ans);
  }

  private static int gcd(int i, int j) {
    if(i < j) return gcd(j, i);
    if(j == 0) return i;
    return gcd(j, i % j);
  }
}