public static void main ( String args [ ] ) {
  Scanner in ;
  PrintStream out ;
  try {
    in = new Scanner ( new FileInputStream ( "java.in" ) ) ;
    out = new PrintStream ( "java.out" ) ;
  }
  catch ( FileNotFoundException e ) {
    in = new Scanner ( new BufferedInputStream ( System . in ) ) ;
    out = System . out ;
  }
  int n = in . nextInt ( ) ;
  int cntOne = 0 ;
  for ( int i = 1 ;
  i <= n ;
  i ++ ) {
    a [ i ] = in . nextInt ( ) ;
    if ( a [ i ] == 1 ) cntOne ++ ;
  }
  if ( cntOne > 0 ) {
    out . println ( n - cntOne ) ;
    return ;
  }
  int ans = - 1 ;
  for ( int i = 1 ;
  i <= n ;
  i ++ ) {
    int c = 0 ;
    for ( int j = i ;
    j <= n ;
    j ++ ) {
      if ( j == i ) c = a [ i ] ;
      else c = gcd ( c , a [ j ] ) ;
      if ( c == 1 ) {
        if ( ans == - 1 ) ans = j - i + n - 1 ;
        else ans = Math . min ( ans , j - i + n - 1 ) ;
      }
    }
  }
  out . println ( ans ) ;
}

public static void main ( String args [ ] ) { Scanner in ; PrintStream out ; try { in = new Scanner ( new FileInputStream ( " java . in " ) ) ; out = new PrintStream ( " java . out " ) ; } catch ( FileNotFoundException e ) { in = new Scanner ( new BufferedInputStream ( System . in ) ) ; out = System . out ; } int n = in . nextInt ( ) ; int cntOne = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { a [ i ] = in . nextInt ( ) ; if ( a [ i ] == 1 ) cntOne ++ ; } if ( cntOne > 0 ) { out . println ( n - cntOne ) ; return ; } int ans = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) { int c = 0 ; for ( int j = i ; j <= n ; j ++ ) { if ( j == i ) c = a [ i ] ; else c = gcd ( c , a [ j ] ) ; if ( c == 1 ) { if ( ans == - 1 ) ans = j - i + n - 1 ; else ans = Math . min ( ans , j - i + n - 1 ) ; } } } out . println ( ans ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier) dimensions: (dimensions))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier))) (try_statement 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 (string_literal))))))) (expression_statement (assignment_expression left: (identifier) right: (object_creation_expression type: (type_identifier) arguments: (argument_list (string_literal)))))) (catch_clause (catch_formal_parameter (catch_type (type_identifier)) name: (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: (field_access object: (identifier) field: (identifier))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (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 (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (expression_statement (update_expression (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier))))) (return_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (unary_expression operand: (decimal_integer_literal)))) (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: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) alternative: (expression_statement (assignment_expression left: (identifier) right: (method_invocation name: (identifier) arguments: (argument_list (identifier) (array_access array: (identifier) index: (identifier))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal)))) consequence: (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)) right: (decimal_integer_literal)))) alternative: (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)) right: (decimal_integer_literal))))))))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))))