import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    }
    int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    }
}
 import java.io.*; io.* java import java.util.*; util.* java public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    }
    int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    }
} Main static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader new BufferedReader(new InputStreamReader(System.in)) public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    } main {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    } Main main = new Main(); main new Main() System.out.println(main.solve()) System.out.println System.out System System.out main.solve() main.solve main String[] args args long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    } solve {
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    } int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans = 0; ans 0 for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        } int a=1; int a=1; a 1 a<=k a k a++ a++ a {
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        } {
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        } for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            } int b=1; int b=1; b 1 b<=k b k b++ b++ b {
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            } {
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            } for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                } int c=1; int c=1; c 1 c<=k c k c++ c++ c {
                    ans += gcd(gcd(a,b),c);
                } {
                    ans += gcd(gcd(a,b),c);
                } ans += gcd(gcd(a,b),c) ans gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c return ans; ans int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    } gcd {
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    } if(a<b){
            int c = a; a=b;b=c;
        } a<b a b {
            int c = a; a=b;b=c;
        } int c = a; c a a=b a b b=c b c while(a%b!=0){
            int c = b; b=a%b;a=c;
        } a%b!=0 a%b a b 0 {
            int c = b; b=a%b;a=c;
        } int c = b; c b b=a%b b a%b a b a=c a c return b; b int a a int b b public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    }
    int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    }
} public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    }
    int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    }
} Main
import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int k = sc.nextInt();
        long ans = 0;
        for(int a=1;a<=k;a++){
            for(int b=1;b<=k;b++){
                for(int c=1;c<=k;c++){
                    ans += gcd(gcd(a,b),c);
                }
            }
        }
        return ans;
    }
    int gcd(int a, int b){
        if(a<b){
            int c = a; a=b;b=c;
        }
        while(a%b!=0){
            int c = b; b=a%b;a=c;
        }
        return b;
    }
}

