import java.io.*;

class Calc{
    public static int gcd(int a, int b, int c)  {
        int r;
        while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        }
        while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        }
        return a;
    }
}

class Main{

    public static void main(String args[])
    {
        String buf;
        int num;
        int ans;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {}
  }
} import java.io.*; io.* java class Calc{
    public static int gcd(int a, int b, int c)  {
        int r;
        while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        }
        while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        }
        return a;
    }
} Calc public static int gcd(int a, int b, int c)  {
        int r;
        while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        }
        while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        }
        return a;
    } gcd {
        int r;
        while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        }
        while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        }
        return a;
    } int r; r while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        } b > 0 b 0 {
            r = a % b;
            a = b;
            b = r;
        } r = a % b r a % b a b a = b a b b = r b r while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        } c > 0 c 0 {
            r = a % c;
            a = c;
            c = r;
        } r = a % c r a % c a c a = c a c c = r c r return a; a int a a int b b int c c class Main{

    public static void main(String args[])
    {
        String buf;
        int num;
        int ans;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {}
  }
} Main public static void main(String args[])
    {
        String buf;
        int num;
        int ans;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {}
  } main {
        String buf;
        int num;
        int ans;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {}
  } String buf; buf int num; num int ans; ans try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {} catch(Exception e) {} Exception e {} {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) buf = br.readLine() buf br.readLine() br.readLine br num = Integer.parseInt(buf) num Integer.parseInt(buf) Integer.parseInt Integer buf ans = 0 ans 0 for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            } int i = 1; int i = 1; i 1 i <= num i num i++ i++ i {
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            } {
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            } for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                } int j = 1; int j = 1; j 1 j <= num j num j++ j++ j {
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                } {
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                } for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    } int k = 1; int k = 1; k 1 k <= num k num k++ k++ k {    
                        ans += Calc.gcd(i,j,k);
                    } {    
                        ans += Calc.gcd(i,j,k);
                    } ans += Calc.gcd(i,j,k) ans Calc.gcd(i,j,k) Calc.gcd Calc i j k System.out.println(Integer.toString(ans)) System.out.println System.out System System.out Integer.toString(ans) Integer.toString Integer ans String args[] args
import java.io.*;

class Calc{
    public static int gcd(int a, int b, int c)  {
        int r;
        while (b > 0) {
            r = a % b;
            a = b;
            b = r;
        }
        while (c > 0) {
            r = a % c;
            a = c;
            c = r;
        }
        return a;
    }
}

class Main{

    public static void main(String args[])
    {
        String buf;
        int num;
        int ans;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buf = br.readLine();
            num = Integer.parseInt(buf);
            ans = 0;
            for(int i = 1; i <= num; i++){
                for(int j = 1; j <= num; j++){
                    for(int k = 1; k <= num; k++){    
                        ans += Calc.gcd(i,j,k);
                    }
                }
            }
            
            System.out.println(Integer.toString(ans));
        } catch(Exception e) {}
  }
}