import java.util.Scanner;

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

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    }



  private static int _gcd (int a, int b) {
	int temp;
	while((temp = a % b) != 0) {
		a = b;
		b = temp;
  }
	return b;
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    }



  private 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(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int sum = 0; sum 0 for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    } 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++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    } {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    } for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      } int b = 1; int b = 1; b 1 b <= n b n b++ b++ b {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      } {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      } for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        } int c = 1; int c = 1; c 1 c <= n c n c++ c++ c {
          sum += _gcd(_gcd(a, b), c);
        } {
          sum += _gcd(_gcd(a, b), c);
        } sum += _gcd(_gcd(a, b), c) sum _gcd(_gcd(a, b), c) _gcd _gcd(a, b) _gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args private 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(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    }



  private 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(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    }



  private static int _gcd (int a, int b) {
	int temp;
	while((temp = a % b) != 0) {
		a = b;
		b = temp;
  }
	return b;
  }
} Main
import java.util.Scanner;

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

    int sum = 0;
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        for (int c = 1; c <= n; c++) {
          sum += _gcd(_gcd(a, b), c);
        }
      }
    }
    System.out.println(sum);
    }



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