import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  }
  
  private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  }
  
  private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  }
  
  private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  }
  
  private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  }
} Main public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  } main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int k = Integer.parseInt(br.readLine()); k Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int sum = 0; sum 0 for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    } {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    } for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      } {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      } for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        } int s = 1; int s = 1; s 1 s <= k s k s++ s++ s {
          sum += gcd(i, j, s);
        } {
          sum += gcd(i, j, s);
        } sum += gcd(i, j, s) sum gcd(i, j, s) gcd i j s System.out.print(sum) System.out.print System.out System System.out sum String[] args args private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  } gcd {
  	return gcdTwo(gcdTwo(i, j), s);
  } return gcdTwo(gcdTwo(i, j), s); gcdTwo(gcdTwo(i, j), s) gcdTwo gcdTwo(i, j) gcdTwo i j s int i i int j j int s s private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  } gcdTwo {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  } if (j == 0) {
      return i;
    } j == 0 j 0 {
      return i;
    } return i; i int r = i % j; r i % j i j return gcdTwo(j, r); gcdTwo(j, r) gcdTwo j r int i i int j j public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  }
  
  private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  }
  
  private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  }
  
  private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  }
  
  private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int k = Integer.parseInt(br.readLine());

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        for (int s = 1; s <= k; s++) {
          sum += gcd(i, j, s);
        }
      }
    }
    
    System.out.print(sum);
  }
  
  private static int gcd(int i, int j, int s) {
  	return gcdTwo(gcdTwo(i, j), s);
  }
  
  private static int gcdTwo(int i, int j) {
  	if (j == 0) {
      return i;
    }
    int r = i % j;
    return gcdTwo(j, r);
  }
}
