import java.util.*;

public class Main {
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    }

    public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }
} 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 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    }

    public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int 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(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++) {
                    ans += gcd(gcd(a,b),c);
                }
            }
        } {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(gcd(a,b),c);
                }
            }
        } for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += 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++) {
                    ans += gcd(gcd(a,b),c);
                }
            } {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(gcd(a,b),c);
                }
            } for (int c = 1; c <= k; c++) {
                    ans += gcd(gcd(a,b),c);
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    ans += gcd(gcd(a,b),c);
                } {
                    ans += gcd(gcd(a,b),c);
                } ans += gcd(gcd(a,b),c) ans gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    } gcd {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    } if(num2==0) return num1;
        else return gcd(num2,num1%num2); num2==0 num2 0 return num1; num1 return gcd(num2,num1%num2); gcd(num2,num1%num2) gcd num2 num1%num2 num1 num2 int num1 num1 int num2 num2 public class Main {
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    }

    public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }
} public class Main {
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    }

    public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }
} Main
import java.util.*;

public class Main {
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int 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(gcd(a,b),c);
                }
            }
        }
		System.out.println(ans);
    }

    public static int gcd(int num1,int num2) {
        if(num2==0) return num1;
        else return gcd(num2,num1%num2);
    }
}
