import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    }

    public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    }

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

    public static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }

} import java.math.BigInteger; BigInteger java.math import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    }

    public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    }

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

    public static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }

} Main public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    } main {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    } BigInteger sum = BigInteger.ZERO; sum BigInteger.ZERO BigInteger BigInteger.ZERO int tmpGcd; tmpGcd Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc Map<String, Integer> tmpMap = new HashMap<>(); tmpMap new HashMap<>() for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        } int a = 1; int a = 1; a 1 a <= N a N a++ a++ a {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        } {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        } for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            } int b = 1; int b = 1; b 1 b <= N b N b++ b++ b {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            } {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            } for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                } int c = 1; int c = 1; c 1 c <= N c N c++ c++ c {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                } {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                } tmpGcd = mygcd(a, b, tmpMap) tmpGcd mygcd(a, b, tmpMap) mygcd a b tmpMap sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap))) sum sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap))) sum.add sum BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)) BigInteger.valueOf BigInteger mygcd(tmpGcd, c, tmpMap) mygcd tmpGcd c tmpMap System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    } mygcd {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    } if (a == b) {
            return a;
        } a == b a b {
            return a;
        } return a; a int tmpGcd; tmpGcd List<Integer> tmp = new ArrayList<Integer>(); tmp new ArrayList<Integer>() tmp.add(a) tmp.add tmp a tmp.add(b) tmp.add tmp b Collections.sort(tmp) Collections.sort Collections tmp String tmpStr = "" + tmp.get(0) + tmp.get(1); tmpStr "" + tmp.get(0) + tmp.get(1) "" + tmp.get(0) + tmp.get(1) "" tmp.get(0) tmp.get tmp 0 tmp.get(1) tmp.get tmp 1 if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        } false && tmpMap.containsKey(tmpStr) false tmpMap.containsKey(tmpStr) tmpMap.containsKey tmpMap tmpStr {
            tmpGcd = tmpMap.get(tmpStr);
        } tmpGcd = tmpMap.get(tmpStr) tmpGcd tmpMap.get(tmpStr) tmpMap.get tmpMap tmpStr {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        } tmpGcd = gcd(a, b) tmpGcd gcd(a, b) gcd a b tmpMap.put(tmpStr, tmpGcd) tmpMap.put tmpMap tmpStr tmpGcd return tmpGcd; tmpGcd int a a int b b Map<String, Integer> tmpMap tmpMap 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 static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    } gcda {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    } int tmp tmp r; r if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        } a < b a b {
            tmp = a;
            a = b;
            b = tmp;
        } tmp = a tmp a a = b a b b = tmp b tmp r = a % b r a % b a b while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        } r != 0 r 0 {
            a = b;
            b = r;
            r = a % b;
        } a = b a b b = r b r r = a % b r a % b a b return b; b int a a int b b public class Main {

    public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    }

    public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    }

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

    public static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }

} public class Main {

    public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    }

    public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    }

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

    public static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }

} Main
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        BigInteger sum = BigInteger.ZERO;
        int tmpGcd;
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        Map<String, Integer> tmpMap = new HashMap<>();
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                for (int c = 1; c <= N; c++) {
                    tmpGcd = mygcd(a, b, tmpMap);
                    sum = sum.add(BigInteger.valueOf(mygcd(tmpGcd, c, tmpMap)));
                }
            }
        }
        System.out.println(sum);
    }

    public static int mygcd(int a, int b, Map<String, Integer> tmpMap) {
        if (a == b) {
            return a;
        }
        int tmpGcd;
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(a);
        tmp.add(b);
        Collections.sort(tmp);
        String tmpStr = "" + tmp.get(0) + tmp.get(1);
        if (false && tmpMap.containsKey(tmpStr)) {
            tmpGcd = tmpMap.get(tmpStr);
        } else {
            tmpGcd = gcd(a, b);
            tmpMap.put(tmpStr, tmpGcd);
        }
        return tmpGcd;
    }

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

    public static int gcda(int a, int b) {
        int tmp, r;
        if (a < b) {
            tmp = a;
            a = b;
            b = tmp;
        }

        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }

}