import java.util.*;
import java.math.BigInteger;

public class Main{
	public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	}

	public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	}
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   }
} import java.util.*; util.* java import java.math.BigInteger; BigInteger java.math public class Main{
	public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	}

	public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	}
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   }
} Main public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	} gcd {
 	return calculateGcd(calculateGcd(a, b), c);
	} return calculateGcd(calculateGcd(a, b), c); calculateGcd(calculateGcd(a, b), c) calculateGcd calculateGcd(a, b) calculateGcd a b c int a a int b b int c c public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	} calculateGcd {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	} if (a == 0) return b; a == 0 a 0 return b; b if (b == 0) return a; b == 0 b 0 return a; a if (a > b) return calculateGcd(b, a % b); a > b a b return calculateGcd(b, a % b); calculateGcd(b, a % b) calculateGcd b a % b a b return calculateGcd(a, b % a); calculateGcd(a, b % a) calculateGcd a b % a b a int a a int b b public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   } main {
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc BigInteger sum = BigInteger.valueOf(0); sum BigInteger.valueOf(0) BigInteger.valueOf BigInteger 0 for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     } int i=1; int i=1; i 1 i<=a i a i++ i++ i {
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     } {
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     } for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       } int j=1; int j=1; j 1 j<=a j a j++ j++ j {
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       } {
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       } for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         } int k=1; int k=1; k 1 k<=a k a k++ k++ k {
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         } {
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         } sum=sum.add(BigInteger.valueOf(gcd(i,j,k))) sum sum.add(BigInteger.valueOf(gcd(i,j,k))) sum.add sum BigInteger.valueOf(gcd(i,j,k)) BigInteger.valueOf BigInteger gcd(i,j,k) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main{
	public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	}

	public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	}
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   }
} public class Main{
	public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	}

	public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	}
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   }
} Main
import java.util.*;
import java.math.BigInteger;

public class Main{
	public static int gcd(int a, int b, int c){
 	return calculateGcd(calculateGcd(a, b), c);
	}

	public static int calculateGcd(int a, int b) {
  	if (a == 0) return b;
   	if (b == 0) return a;
    if (a > b) return calculateGcd(b, a % b);
    return calculateGcd(a, b % a);
 	}
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
     BigInteger sum = BigInteger.valueOf(0);
     for(int i=1;i<=a;i++){
       for(int j=1;j<=a;j++){
         for(int k=1;k<=a;k++){
           sum=sum.add(BigInteger.valueOf(gcd(i,j,k)));
         }
       }
     }
     System.out.println(sum);
   }
}