import java.util.*;


class Main {
  public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
    
    int N = scanner.nextInt(); //next(文字列), nextDouble, nextLine(1行)とか
    int ans = 0;
    for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    }
    System.out.println(ans);
  }

  public static int GCD(int x, int y, int z) {
    
    while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    }
    
    while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    }    
    return z;
  }


}

 import java.util.*; util.* java class Main {
  public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
    
    int N = scanner.nextInt(); //next(文字列), nextDouble, nextLine(1行)とか
    int ans = 0;
    for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    }
    System.out.println(ans);
  }

  public static int GCD(int x, int y, int z) {
    
    while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    }
    
    while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    }    
    return z;
  }


} Main public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
    
    int N = scanner.nextInt(); //next(文字列), nextDouble, nextLine(1行)とか
    int ans = 0;
    for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    }
    System.out.println(ans);
  } main {
	Scanner scanner = new Scanner(System.in);
    
    int N = scanner.nextInt(); //next(文字列), nextDouble, nextLine(1行)とか
    int ans = 0;
    for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    }
    System.out.println(ans);
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int N = scanner.nextInt(); N scanner.nextInt() scanner.nextInt scanner int ans = 0; ans 0 for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    } int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    } {
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    } for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      } int j = 1; int j = 1; j 1 j <= N j N j++ j++ j {
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      } {
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      } for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        } int k = 1; int k = 1; k 1 k <= N k N k++ k++ k {
          ans += GCD(i,j,k);
        } {
          ans += GCD(i,j,k);
        } ans += GCD(i,j,k) ans GCD(i,j,k) GCD i j k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static int GCD(int x, int y, int z) {
    
    while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    }
    
    while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    }    
    return z;
  } GCD {
    
    while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    }
    
    while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    }    
    return z;
  } while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    } x != y x y {
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    } if (x > y){
        x = x-y;
      }else{
        y = y -x;
      } x > y x y {
        x = x-y;
      } x = x-y x x-y x y {
        y = y -x;
      } y = y -x y y -x y x while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    } x != z x z {
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    } if (x > z){
        x = x-z;
      }else{
        z = z -x;
      } x > z x z {
        x = x-z;
      } x = x-z x x-z x z {
        z = z -x;
      } z = z -x z z -x z x return z; z int x x int y y int z z
import java.util.*;


class Main {
  public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
    
    int N = scanner.nextInt(); //next(文字列), nextDouble, nextLine(1行)とか
    int ans = 0;
    for (int i = 1; i <= N ; i++){
      for (int j = 1; j <= N; j++){
        for (int k = 1; k <= N; k++){
          ans += GCD(i,j,k);
        }
      }
    }
    System.out.println(ans);
  }

  public static int GCD(int x, int y, int z) {
    
    while (x != y){
      if (x > y){
        x = x-y;
      }else{
        y = y -x;
      }
    }
    
    while (x != z){
      if (x > z){
        x = x-z;
      }else{
        z = z -x;
      }
    }    
    return z;
  }


}


