import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
public class Main {
	public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
}

static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
}


} import java.io.BufferedReader; BufferedReader java.io import java.io.*; io.* java import java.util.*; util.* java import java.util.ArrayList; ArrayList java.util import java.util.Iterator; Iterator java.util import java.util.List; List java.util import java.util.Collections; Collections java.util public class Main {
	public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
}

static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
}


} Main public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
} main {
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    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 m = 0; m 0 List<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    } int i = 1; int i = 1; i 1 i < k + 1 i k + 1 k 1 i++ i++ i {
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    } {
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    } for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        } int j = 1; int j = 1; j 1 j < k + 1 j k + 1 k 1 j++ j++ j {
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        } {
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        } for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            } int l = 1; int l = 1; l 1 l < k + 1 l k + 1 k 1 l++ l++ l {
                m = gcd(i,j);
                list.add(gcd(m,l));
            } {
                m = gcd(i,j);
                list.add(gcd(m,l));
            } m = gcd(i,j) m gcd(i,j) gcd i j list.add(gcd(m,l)) list.add list gcd(m,l) gcd m l int sum = 0; sum 0 for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    } int i = 0 ; int i = 0 ; i 0 i < k*k*k i k*k*k k*k*k k k k i++ i++ i {
        sum += list.get(i);
    } {
        sum += list.get(i);
    } sum += list.get(i) sum list.get(i) list.get list i System.out.println(sum) System.out.println System.out System System.out sum final String[] args args static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
} gcd {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
} int temp; temp while((temp = a%b)!=0) {
		a = b;
		b = temp;
	} (temp = a%b)!=0 (temp = a%b) temp a%b a b 0 {
		a = b;
		b = temp;
	} a = b a b b = temp b temp return b; b int a a int b b public class Main {
	public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
}

static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
}


} public class Main {
	public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
}

static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
}


} Main
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
public class Main {
	public static void main(final String[] args){
    
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int m = 0;
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i < k + 1; i++){
        for(int j = 1; j < k + 1; j++){
            for(int l = 1; l < k + 1; l++){
                m = gcd(i,j);
                list.add(gcd(m,l));
            }
        }
    }
    int sum = 0;
    for(int i = 0 ; i < k*k*k ; i++){
        sum += list.get(i);
    }
    System.out.println(sum);
    
}

static int gcd (int a, int b) {
	int temp;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
}


}