public static void main ( String [ ] $ ) {
  int n = scanner . nextInt ( ) , m = scanner . nextInt ( ) , d [ ] [ ] = IntStream . range ( 0 , n ) . mapToObj ( i -> IntStream . range ( 0 , n ) . map ( j -> i == j ? 0 : 0x1fffffff ) . toArray ( ) ) . toArray ( int [ ] [ ] :: new ) ;
  for ( int i = 0 , u1 , u2 ;
  i < m ;
  i ++ ) d [ u1 = scanner . nextInt ( ) - 1 ] [ u2 = scanner . nextInt ( ) - 1 ] = d [ u2 ] [ u1 ] = Math . min ( d [ u1 ] [ u2 ] , scanner . nextInt ( ) ) ;
  IntStream . range ( 1 , n ) . forEach ( i -> IntStream . range ( 1 , n ) . forEach ( j -> IntStream . range ( 1 , n ) . forEach ( k -> d [ j ] [ k ] = Math . min ( d [ j ] [ k ] , d [ j ] [ i ] + d [ i ] [ k ] ) ) ) ) ;
  System . out . println ( IntStream . range ( 1 , n ) . mapToObj ( i -> IntStream . range ( i + 1 , n ) . map ( j -> d [ 0 ] [ i ] + d [ i ] [ j ] + d [ j ] [ 0 ] ) . min ( ) ) . filter ( OptionalInt :: isPresent ) . min ( Comparator . comparing ( OptionalInt :: getAsInt ) ) . filter ( o -> o . getAsInt ( ) < 0x1fffffff ) . orElse ( OptionalInt . of ( - 1 ) ) . getAsInt ( ) ) ;
}

public static void main ( String [ ] $ ) { int n = scanner . nextInt ( ) , m = scanner . nextInt ( ) , d [ ] [ ] = IntStream . range ( 0 , n ) . mapToObj ( i -> IntStream . range ( 0 , n ) . map ( j -> i == j ? 0 : 0x1fffffff ) . toArray ( ) ) . toArray ( int [ ] [ ] :: new ) ; for ( int i = 0 , u1 , u2 ; i < m ; i ++ ) d [ u1 = scanner . nextInt ( ) - 1 ] [ u2 = scanner . nextInt ( ) - 1 ] = d [ u2 ] [ u1 ] = Math . min ( d [ u1 ] [ u2 ] , scanner . nextInt ( ) ) ; IntStream . range ( 1 , n ) . forEach ( i -> IntStream . range ( 1 , n ) . forEach ( j -> IntStream . range ( 1 , n ) . forEach ( k -> d [ j ] [ k ] = Math . min ( d [ j ] [ k ] , d [ j ] [ i ] + d [ i ] [ k ] ) ) ) ) ; System . out . println ( IntStream . range ( 1 , n ) . mapToObj ( i -> IntStream . range ( i + 1 , n ) . map ( j -> d [ 0 ] [ i ] + d [ i ] [ j ] + d [ j ] [ 0 ] ) . min ( ) ) . filter ( OptionalInt :: isPresent ) . min ( Comparator . comparing ( OptionalInt :: getAsInt ) ) . filter ( o -> o . getAsInt ( ) < 0x1fffffff ) . orElse ( OptionalInt . of ( - 1 ) ) . getAsInt ( ) ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (ternary_expression condition: (binary_expression left: (identifier) right: (identifier)) consequence: (decimal_integer_literal) alternative: (hex_integer_literal))))) name: (identifier) arguments: (argument_list))))) name: (identifier) arguments: (argument_list (method_reference (array_type element: (integral_type) dimensions: (dimensions))))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier)) declarator: (variable_declarator name: (identifier))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (assignment_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) index: (assignment_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) right: (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))))) (expression_statement (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))))))))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (method_invocation object: (method_invocation object: (method_invocation object: (method_invocation object: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (binary_expression left: (binary_expression left: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (identifier)) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier))) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)))))) name: (identifier) arguments: (argument_list))))) name: (identifier) arguments: (argument_list (method_reference (identifier) (identifier)))) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_reference (identifier) (identifier)))))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (hex_integer_literal))))) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (unary_expression operand: (decimal_integer_literal)))))) name: (identifier) arguments: (argument_list)))))))