import java.util.Scanner;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    }
    public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    }
    public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    }
} Main public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    } main {
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int count = 0; count 0 for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += 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++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	} {
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	} for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += 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++){
		    count += gcd(gcd(a,b),c);
		}
	    } {
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    } for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		} int c=1; int c=1; c 1 c<=K c K c++ c++ c {
		    count += gcd(gcd(a,b),c);
		} {
		    count += gcd(gcd(a,b),c);
		} count += gcd(gcd(a,b),c) count gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c System.out.print(count) System.out.print System.out System System.out count String[] args args public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    } gcd {
	if(p%q==0) return q;
	return gcd(q,p%q);
    } if(p%q==0) return q; p%q==0 p%q p q 0 return q; q return gcd(q,p%q); gcd(q,p%q) gcd q p%q p q int p p int q q public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    }
    public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    }
} public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    }
    public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    }
} Main
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int K = sc.nextInt();
	int count = 0;
	for(int a=1;a<=K;a++){
	    for(int b=1;b<=K;b++){
		for(int c=1;c<=K;c++){
		    count += gcd(gcd(a,b),c);
		}
	    }
	}
	System.out.print(count);
    }
    public static int gcd(int p,int q){
	if(p%q==0) return q;
	return gcd(q,p%q);
    }
}
