import java.util.*;
public class Main {

  private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  }

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

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

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

  private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  }

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

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

  }
} Main private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  } gcd {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  } if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if(n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n long m m long n n public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

  } main {
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int ans = 0; ans 0 for (int a = 1; a <= k; a++) {
      for (int b = 1; b <= k; b++) {
        for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(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++) {
          ans += gcd(a, gcd(b,c));
        }
      }
    } {
      for (int b = 1; b <= k; b++) {
        for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(b,c));
        }
      }
    } for (int b = 1; b <= k; b++) {
        for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(b,c));
        }
      } int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
        for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(b,c));
        }
      } {
        for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(b,c));
        }
      } for (int c = 1; c <= k; c++) {
          ans += gcd(a, gcd(b,c));
        } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
          ans += gcd(a, gcd(b,c));
        } {
          ans += gcd(a, gcd(b,c));
        } ans += gcd(a, gcd(b,c)) ans gcd(a, gcd(b,c)) gcd a gcd(b,c) gcd b c System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String args[] args public class Main {

  private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  }

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

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

  }
} public class Main {

  private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  }

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

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

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

  private static long gcd(long m, long n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
  }

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

    int k = sc.nextInt();
    int ans = 0;

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

    sc.close();

  }
}