import java.util.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    }
    
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} import java.util.*; util.* java import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    }
    
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    } main {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) String input = scanner.nextLine(); input scanner.nextLine() scanner.nextLine scanner int K = Integer.parseInt(input); K Integer.parseInt(input) Integer.parseInt Integer input 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(a, gcd(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(a, gcd(b, c));
                }
            }
        } {
            for(int b = 1; b <= K; b++){
                for(int c = 1; c <= K; c++){
                    ans += gcd(a, gcd(b, c));
                }
            }
        } for(int b = 1; b <= K; b++){
                for(int c = 1; c <= K; c++){
                    ans += gcd(a, gcd(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(a, gcd(b, c));
                }
            } {
                for(int c = 1; c <= K; c++){
                    ans += gcd(a, gcd(b, c));
                }
            } for(int c = 1; c <= K; c++){
                    ans += gcd(a, gcd(b, c));
                } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
                    ans += gcd(a, gcd(b, c));
                } {
                    ans += gcd(a, gcd(b, c));
                } ans += gcd(a, gcd(b, c)) ans gcd(a, gcd(b, c)) gcd a gcd(b, c) gcd b c System.out.println(ans) System.out.println System.out System System.out ans String[] args args private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    } gcd {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    } if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if(n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n long m m long n n public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    }
    
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    }
    
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} Main
import java.util.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int K = Integer.parseInt(input);
        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(a, gcd(b, c));
                }
            }
        }
        
        System.out.println(ans);
    }
    
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
}
