import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    }
    
    public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    }
    
    public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        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 long sum = 0; sum 0 for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        } 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++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        } {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        } for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            } int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            } {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            } for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                } {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                } if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    } a == b && b ==c a == b a b b ==c b c {
                        sum += a;
                    } sum += a sum a {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    } int buf = gcd(a,b,c); buf gcd(a,b,c) gcd a b c sum += buf sum buf System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    } gcd {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    } if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        } x < y x y {
            int buf = x;
            x = y;
            y = buf;
        } int buf = x; buf x x = y x y y = buf y buf int r = 0; r 0 while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        } ( r = x % y) != 0 ( r = x % y) r x % y x y 0 {
            x = y;
            y = r;
        } x = y x y y = r y r if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        } y < z y z {
            int buf = y;
            y = z;
            z = buf;
        } int buf = y; buf y y = z y z z = buf z buf while((r = y % z) != 0)
        {
            y = z;
            z = r;
        } (r = y % z) != 0 (r = y % z) r y % z y z 0 {
            y = z;
            z = r;
        } y = z y z z = r z r return z; z int x x int y y int z z public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    }
    
    public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    }
    
    public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        long sum = 0;
        
        for(int a = 1; a <= k; a++)
        {
            for(int b = 1; b <= k; b++)
            {
                for(int c = 1; c <= k; c++)
                {
                    if(a == b && b ==c)
                    {
                        sum += a;
                    }
                    else
                    {
                        //最大公約数を求める
                        int buf = gcd(a,b,c);
                        sum += buf;
                    }
                }
            }
        }
        System.out.println(sum);
    }
    
    public static int gcd(int x,int y, int z) {
        if(x < y)
        {
            int buf = x;
            x = y;
            y = buf;
        }
        int r = 0;
        while(( r = x % y) != 0)
        {
            x = y;
            y = r;
        }
        
        if(y < z)
        {
            int buf = y;
            y = z;
            z = buf;
        }
        while((r = y % z) != 0)
        {
            y = z;
            z = r;
        }
        return z;
    }
}
