import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

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

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

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

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
} Main public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

    long sum = 0;
    for(int a=1; a<=N; a++) {
      for(int b=1; b<=N; b++) {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      }
    }
    System.out.println(sum);
  } main {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

    long sum = 0;
    for(int a=1; a<=N; a++) {
      for(int b=1; b<=N; b++) {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      }
    }
    System.out.println(sum);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan scan.close() scan.close scan long sum = 0; sum 0 for(int a=1; a<=N; a++) {
      for(int b=1; b<=N; b++) {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(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++) {
          sum += gcd(a, gcd(b,c));
        }
      }
    } {
      for(int b=1; b<=N; b++) {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      }
    } for(int b=1; b<=N; b++) {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      } int b=1; int b=1; b 1 b<=N b N b++ b++ b {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      } {
        for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        }
      } for(int c=1; c<=N; c++) {
          sum += gcd(a, gcd(b,c));
        } int c=1; int c=1; c 1 c<=N c N c++ c++ c {
          sum += gcd(a, gcd(b,c));
        } {
          sum += gcd(a, gcd(b,c));
        } sum += gcd(a, gcd(b,c)) sum gcd(a, gcd(b,c)) gcd a gcd(b,c) gcd b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  } gcd {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  } int r = a % b; r a % b a b while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    } r != 0 r 0 {
      a = b;
      b = r;
      r = a % b;
    } a = b a b b = r b r r = a % b r a % b a b return b; b int a a int b b public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

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

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
} public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

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

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
} Main
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();

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

  public static int gcd(int a, int b) {
    int r = a % b;
    while(r != 0) {
      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
}