import java.util.*;

class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int out = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        }
        System.out.println(out);
        scan.close();
    }
    
    int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b, a % b);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} import java.util.*; util.* java class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int out = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        }
        System.out.println(out);
        scan.close();
    }
    
    int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b, a % b);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} Main void solve(){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int out = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        }
        System.out.println(out);
        scan.close();
    } solve {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int out = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        }
        System.out.println(out);
        scan.close();
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int out = 0; out 0 for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += 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++){
                    out += gcd(gcd(a, b), c);
                }
            }
        } {
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        } for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += 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++){
                    out += gcd(gcd(a, b), c);
                }
            } {
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            } for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    out += gcd(gcd(a, b), c);
                } {
                    out += gcd(gcd(a, b), c);
                } out += gcd(gcd(a, b), c) out gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c System.out.println(out) System.out.println System.out System System.out out scan.close() scan.close scan 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 void main(String[] args){
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args
import java.util.*;

class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int out = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    out += gcd(gcd(a, b), c);
                }
            }
        }
        System.out.println(out);
        scan.close();
    }
    
    int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b, a % b);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
}
