import java.util.*;
public class Main{
  static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
}
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  }
} import java.util.*; util.* java public class Main{
  static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
}
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  }
} Main static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
} gcd {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
} long r; r while((r = a%b)!=0) {
		a = b;
		b = r;
	} (r = a%b)!=0 (r = a%b) r a%b a b 0 {
		a = b;
		b = r;
	} a = b a b b = r b r return b; b long a a long b b public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  } main {
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  } Scanner sc =new Scanner(System.in); sc new Scanner(System.in) int K=sc.nextInt(); K sc.nextInt() sc.nextInt sc long x=0; x 0 for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    } int i=1; int i=1; i 1 i<K+1 i K+1 K 1 i++ i++ i {
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    } {
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    } for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      } int j=1; int j=1; j 1 j<K+1 j K+1 K 1 j++ j++ j {
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      } {
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      } for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        } int k=1; int k=1; k 1 k<K+1 k K+1 K 1 k++ k++ k {
          long t =gcd(j,k);
           x =x+gcd(i,t);
        } {
          long t =gcd(j,k);
           x =x+gcd(i,t);
        } long t =gcd(j,k); t gcd(j,k) gcd j k x =x+gcd(i,t) x x+gcd(i,t) x gcd(i,t) gcd i t System.out.println(x) System.out.println System.out System System.out x String[] args args public class Main{
  static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
}
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  }
} public class Main{
  static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
}
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  }
} Main
import java.util.*;
public class Main{
  static long gcd (long a, long b) {
	long r;
	while((r = a%b)!=0) {
		a = b;
		b = r;
	}
	return b;
}
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int K=sc.nextInt();
    long x=0;
    for(int i=1;i<K+1;i++){
      for(int j=1;j<K+1;j++){
        for(int k=1;k<K+1;k++){
          long t =gcd(j,k);
           x =x+gcd(i,t);
        }
      }
    }
    System.out.println(x);
  }
}