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

public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    }

    public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    }
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    }

    public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    }
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main static StringTokenizer st; st static BufferedReader br; br static PrintWriter out; out public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    } main {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) int K = nextInt(); K nextInt() nextInt long sum = 0; sum 0 for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        } int i=1; int i=1; i 1 i<=K i K i++ i++ i {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        } {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        } for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            } int j=1; int j=1; j 1 j<=K j K j++ j++ j {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            } {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            } for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                } int k=1; int k=1; k 1 k<=K k K k++ k++ k {
                    sum += gcd(j, gcd(i, k));
                } {
                    sum += gcd(j, gcd(i, k));
                } sum += gcd(j, gcd(i, k)) sum gcd(j, gcd(i, k)) gcd j gcd(i, k) gcd i k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    } gcd {
        if(b==0) return a;

        return gcd(b, a%b);
    } if(b==0) return a; b==0 b 0 return a; a return gcd(b, a%b); gcd(b, a%b) gcd b a%b a b int a a int b b public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    } nextInt {
        return Integer.parseInt(next());
    } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public static long nextLong() throws IOException {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    }

    public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    }
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    }

    public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    }
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main
import java.util.*; 
import java.io.*;

public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        
        long sum = 0;

        for (int i=1; i<=K; i++) {
            for (int j=1; j<=K; j++) {
                for (int k=1; k<=K; k++) {
                    sum += gcd(j, gcd(i, k));
                }
            }
        }
        System.out.println(sum);
    }

    public static int gcd(int a, int b) {
        if(b==0) return a;

        return gcd(b, a%b);
    }
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
}