import java.util.*;

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        
        int a = 0;
        int b = 0;
        int c = 0;
        int 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(gcd(i, j), k);
                }   
            }
        }
        System.out.println(sum);
        
    }
    static int gcd(int x, int y) {
        int amari = 0;
        if(x % y == 0) {
            return y;
        }else {
            amari = x % y;
            return gcd(y, amari);
        }
    }
}