import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long ans = 0; ans 0 for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    } long a = 1; long a = 1; a 1 a <= k a k a++ a++ a {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    } {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    } for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      } long b = 1; long b = 1; b 1 b <= k b k b++ b++ b {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      } {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      } for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        } long c = 1; long c = 1; c 1 c <= k c k c++ c++ c {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        } {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        } long t = gcd(a, b); t gcd(a, b) gcd a b t = gcd(t, c) t gcd(t, c) gcd t c ans += t ans t System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  } gcd {
    if(b == 0) return a;
    return gcd(b, (a % b));
  } if(b == 0) return a; b == 0 b 0 return a; a return gcd(b, (a % b)); gcd(b, (a % b)) gcd b (a % b) a b long a a long b b public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    long ans = 0;
    for(long a = 1; a <= k; a++) {
      for(long b = 1; b <= k; b++) {
        for(long c = 1; c <= k; c++) {
          long t = gcd(a, b);
          t = gcd(t, c);
          ans += t;
        }
      }
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
}