import java.util.Scanner;

public class Main {
  public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  }
} Main public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  } gcd {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  } if (x % y == 0) {
      return y;
    } x % y == 0 x % y x y 0 {
      return y;
    } return y; y return gcd(y, x % y); gcd(y, x % y) gcd y x % y x y int x x int y y public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  } main {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  } Scanner input = new Scanner(System.in); input new Scanner(System.in) int num = Integer.parseInt(input.nextLine()); num Integer.parseInt(input.nextLine()) Integer.parseInt Integer input.nextLine() input.nextLine input long sum = 0; sum 0 for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    } int i = 1; int i = 1; i 1 i <= num i num i++ i++ i {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    } {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    } for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      } int j = 1; int j = 1; j 1 j <= num j num j++ j++ j {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      } {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      } for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        } int k = 1; int k = 1; k 1 k <= num k num k++ k++ k {
          sum += gcd(gcd(i,j),k);
        } {
          sum += gcd(gcd(i,j),k);
        } sum += gcd(gcd(i,j),k) sum gcd(gcd(i,j),k) gcd gcd(i,j) gcd i j k System.out.print(sum) System.out.print System.out System System.out sum String[] args args public class Main {
  public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  }
} public class Main {
  public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  }
} Main
import java.util.Scanner;

public class Main {
  public static int gcd(int x, int y) {
  	if (x % y == 0) {
      return y;
    }
    return gcd(y, x % y);
  }
  
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = Integer.parseInt(input.nextLine());
    long sum = 0;
    
    // System.out.println(gcd(390, 273));
    
    for (int i = 1; i <= num; i++) {
      for (int j = 1; j <= num; j++) {
        for (int k = 1; k <= num; k++) {
          sum += gcd(gcd(i,j),k);
        }
      }
    }
    
    System.out.print(sum);
  }
}