import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  }
  public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  }
} import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  }
  public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  } main {
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long sum = 0; sum 0 for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    } int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    } {
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    } for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      } int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      } {
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      } int sur_1 = gcd(a,b); sur_1 gcd(a,b) gcd a b for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
          sum += gcd(sur_1,c);
        } {
          sum += gcd(sur_1,c);
        } sum += gcd(sur_1,c) sum gcd(sur_1,c) gcd sur_1 c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  } gcd {
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  } int big = Math.max(x,y); big Math.max(x,y) Math.max Math x y int small = Math.min(x,y); small Math.min(x,y) Math.min Math x y int surplus = big%small; surplus big%small big small if(surplus == 0){
      return small;
    } surplus == 0 surplus 0 {
      return small;
    } return small; small surplus = gcd(small,surplus) surplus gcd(small,surplus) gcd small surplus return surplus; surplus int x x int y y public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  }
  public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  }
  public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  }
} Main
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long sum = 0;
    for(int a = 1; a <= k; a++){
      for(int b = 1; b <= k; b++){
        int sur_1 = gcd(a,b);
        for(int c = 1; c <= k; c++){
          sum += gcd(sur_1,c);
        }
      }
    }
    System.out.println(sum);
  }
  public static int gcd(int x, int y){
    int big = Math.max(x,y);
    int small = Math.min(x,y);
    int surplus = big%small;
    if(surplus == 0){
      return small;
    }
    surplus = gcd(small,surplus);
    return surplus;
  }
}