#import <Foundation/Foundation.h>
#import <stdio.h>

@interface Solution: NSObject
- (NSInteger)gcd: (NSInteger)a b: (NSInteger)b;
@end
@implementation Solution
- (NSInteger)gcd: (NSInteger)a b: (NSInteger)b {
    if(b == 0) {
        return a;
    }
    return [self gcd: b b: a % b];
}
@end
int main(int argc, char **argv) {
  NSInteger K;
  scanf("%ld", &K);

  Solution *s = [[Solution alloc] init];
 
  NSInteger sum = 0;
  for(NSInteger i = 1; i <= K; i++) {
      for(NSInteger j = 1; j <= K; j++) {
	      for(NSInteger k = 1; k <= K; k++) {
	          sum += [s gcd: i b: [s gcd: j b: k]];
	      }
      }
  }
  
  printf("%ld", sum); 
  
  return 0;
}