import java.io.IOException ;
import java.util.Arrays ;
import java.util.Scanner ;
public class Main {
Scanner sc
= new Scanner
( System .
in ) ;
int n = sc.nextInt ( ) , m = sc.nextInt ( ) ;
int [ ] leftMost = new int [ n] , rightMost = new int [ n] ;
int maxFloor = - 1 ;
for ( int i = n - 1 ; i >= 0 ; -- i)
{
for ( int j = 0 ; j < m + 2 ; ++ j)
if ( s.charAt ( j) == '1' )
{
rightMost[ i] = j;
if ( maxFloor == - 1 )
maxFloor = i;
}
for ( int j = m + 1 ; j >= 0 ; -- j)
if ( s.charAt ( j) == '1' )
leftMost[ i] = j;
}
int ans = 10000000 ;
for ( int stairs = 0 ; stairs < ( 1 << n - 1 ) ; ++ stairs)
{
int cur = 0 , room = 0 , floor = 0 ;
while ( floor <= maxFloor)
{
if ( room == 0 )
{
cur += rightMost[ floor] - room;
room = rightMost[ floor] ;
}
else
{
cur += room - leftMost[ floor] ;
room = leftMost[ floor] ;
}
if ( floor == maxFloor)
break ;
int nxtStairs = ( stairs & ( 1 << floor) ) == 0 ? 0 : m + 1 ;
cur
+= Math .
abs ( nxtStairs
- room
) + 1 ;
room = nxtStairs;
++ floor;
}
ans
= Math .
min ( ans, cur
) ; }
sc.close ( ) ;
}
}
aW1wb3J0IGphdmEuaW8uSU9FeGNlcHRpb247CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlzOwppbXBvcnQgamF2YS51dGlsLlNjYW5uZXI7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CgoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIElPRXhjZXB0aW9uIHsKCgkJU2Nhbm5lciBzYyA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CgkJCgkJaW50IG4gPSBzYy5uZXh0SW50KCksIG0gPSBzYy5uZXh0SW50KCk7CgkJCgkJaW50W10gbGVmdE1vc3QgPSBuZXcgaW50W25dLCByaWdodE1vc3QgPSBuZXcgaW50W25dOwoJCWludCBtYXhGbG9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