#NEWLINE# def fannkuch( n ) : #NEWLINE# #INDENT# count = range ( 1 , ( m + 1 ) ) #NEWLINE# max_flips = 0 #NEWLINE# m = ( n - 1 ) #NEWLINE# r = n #NEWLINE# check = 0 #NEWLINE# perm1 = range ( n ) #NEWLINE# perm = range ( n ) #NEWLINE# if ( PYTHON == 'PYTHON3' ) : #NEWLINE# #INDENT# count = list ( count ) #NEWLINE# perm1 = list ( perm1 ) #NEWLINE# perm = list ( perm ) #NEWLINE# #UNINDENT# while True : #NEWLINE# #INDENT# if ( check < 30 ) : #NEWLINE# #INDENT# check += 1 #NEWLINE# #UNINDENT# while ( r != 1 ) : #NEWLINE# #INDENT# count [ ( r - 1 ) ] = r #NEWLINE# r -= 1 #NEWLINE# #UNINDENT# if ( ( perm1 [ 0 ] != 0 ) and ( perm1 [ m ] != m ) ) : #NEWLINE# #INDENT# perm = perm1 [ : ] #NEWLINE# flips_count = 0 #NEWLINE# k = perm [ 0 ] #NEWLINE# while ( k != 0 ) : #NEWLINE# #INDENT# perm [ : ( k + 1 ) ] = perm [ k : : ( - 1 ) ] #NEWLINE# flips_count += 1 #NEWLINE# k = perm [ 0 ] #NEWLINE# #UNINDENT# if ( flips_count > max_flips ) : #NEWLINE# #INDENT# max_flips = flips_count #NEWLINE# #UNINDENT# #UNINDENT# while ( r != n ) : #NEWLINE# #INDENT# perm1 . insert ( r , perm1 . pop ( 0 ) ) #NEWLINE# count [ r ] -= 1 #NEWLINE# if ( count [ r ] > 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# r += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return max_flips