import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    }
    public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    }
    public static int pow(int N){
        return N*N*N;
    }
    public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    }
} 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();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    }
    public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    }
    public static int pow(int N){
        return N*N*N;
    }
    public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        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 int alpha=0; alpha 0 int sum =0; sum 0 for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,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++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        } {
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        } for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            } int j=1; int j=1; j 1 j<=N j N j++ j++ j {
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            } {
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            } for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                } int k=1; int k=1; k 1 k<=N k N k++ k++ k {
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                } {
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                } alpha=gcd(i,j) alpha gcd(i,j) gcd i j sum += gcd(alpha,k) sum gcd(alpha,k) gcd alpha k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    } fac {
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    } if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        } N == 1 N 1 {
            return 1;
        } return 1; 1 {
            return fac(N -1)*N;
        } return fac(N -1)*N; fac(N -1)*N fac(N -1) fac N -1 N 1 N int N N public static int pow(int N){
        return N*N*N;
    } pow {
        return N*N*N;
    } return N*N*N; N*N*N N*N*N N N N int N N public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    } gcd {
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    } int c = a%b; c a%b a b if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        } c == 0 c 0 {
            return b;
        } return b; b {
            return gcd(b,c);
        } return gcd(b,c); gcd(b,c) gcd b c 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();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    }
    public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    }
    public static int pow(int N){
        return N*N*N;
    }
    public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    }
} public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    }
    public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    }
    public static int pow(int N){
        return N*N*N;
    }
    public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    }
} Main
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //int Nall = pow(fac(N));
        //int[][] matrixArray = new int[4][Nall];
        int alpha=0;
        int sum =0;

        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                for(int k=1;k<=N;k++){
                    //System.out.println(k);
                    alpha=gcd(i,j);
                    sum += gcd(alpha,k);
                }
            }
        }
        System.out.println(sum);
    }
    public static int fac(int N){
        if(N == 1){
            return 1;
        }else{
            return fac(N -1)*N;
        }
    }
    public static int pow(int N){
        return N*N*N;
    }
    public static int gcd(int a,int b){
        int c = a%b;
        if(c == 0){
            return b;
        }else{
            return gcd(b,c);
        }
    }
}