public static void main ( String [ ] args ) throws IOException {
  br = new BufferedReader ( new InputStreamReader ( System . in ) ) ;
  pw = new PrintWriter ( System . out ) ;
  int p = nextInt ( ) ;
  int k = nextInt ( ) ;
  long ans = 1 ;
  boolean used [ ] = new boolean [ p ] ;
  for ( int i = 1 ;
  i < p ;
  i ++ ) {
    int v = i ;
    if ( ! used [ i ] ) {
      while ( ! used [ v ] ) {
        used [ v ] = true ;
        v = ( int ) ( ( ( long ) v * k ) % p ) ;
      }
      ans = ( ans * p ) % inf ;
    }
  }
  if ( k == 1 ) ans = ( ans * p ) % inf ;
  pw . println ( ans ) ;
  pw . close ( ) ;
}

public static void main ( String [ ] args ) throws IOException { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; pw = new PrintWriter ( System . out ) ; int p = nextInt ( ) ; int k = nextInt ( ) ; long ans = 1 ; boolean used [ ] = new boolean [ p ] ; for ( int i = 1 ; i < p ; i ++ ) { int v = i ; if ( ! used [ i ] ) { while ( ! used [ v ] ) { used [ v ] = true ; v = ( int ) ( ( ( long ) v * k ) % p ) ; } ans = ( ans * p ) % inf ; } } if ( k == 1 ) ans = ( ans * p ) % inf ; pw . println ( ans ) ; pw . close ( ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) (throws (type_identifier)) body: (block (expression_statement (assignment_expression left: (identifier) right: (object_creation_expression type: (type_identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))))) (expression_statement (assignment_expression left: (identifier) right: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (boolean_type) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (if_statement condition: (parenthesized_expression (unary_expression operand: (array_access array: (identifier) index: (identifier)))) consequence: (block (while_statement condition: (parenthesized_expression (unary_expression operand: (array_access array: (identifier) index: (identifier)))) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (true))) (expression_statement (assignment_expression left: (identifier) right: (cast_expression type: (integral_type) value: (parenthesized_expression (binary_expression left: (parenthesized_expression (binary_expression left: (cast_expression type: (integral_type) value: (identifier)) right: (identifier))) right: (identifier)))))))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))