//By the name of God
/*.....   .
      .   .
  ... . ...
  .   .
  .   .....*/
//In mathematics, an unordered pair or pair set is a set of the form {a, b},
//i.e. a set having two elements a and b with no particular relation between them.
//In contrast, an ordered pair (a, b) has a as its first element and b as its second element.
//So, (a,b)!=(b,a) in ordered pair but it is in case of unordered pair.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define   ll  long long int
#define   dl double
#define   llu unsigned long long int
#define   scll(x) scanf("%lld",&x)
#define   scld(x) scanf("%lf",&x)
#define   scull(x) scanf("%llu",&x)
#define   scch(x) scanf(" %c",&x)
#define   eb(x)   emplace_back(x)
#define   pb(x) push_back(x)
#define   pf(x) push_front(x)
#define   ppb() pop_back()
#define   ppf() pop_front()
#define   Pi acos(-1.0)
#define   inf 1000000000000000000
#define   mod 1000000007
#define   ff first
#define   ss second
#define   mpr make_pair
#define   debug(x) cout<<x<<'\n';
#define   MAX 1000000
#define   eps 1e-8
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//Taking input by EOF
//while(scanf("%lld",&x)!=-1){ }
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
//pair can no te used as index in Unordered_map
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
int main()
{
/*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif*/
  ll a, b, c = 1, d, e, f = 0, g, h, i, j = 0, k, m, n, o, p, s, t, v, w, x = 0, y, z, l, res = 0;
  ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = inf;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2, str3 = "", str4 = "";
  ll ide = 0, idx = 0, Q;
  ll xa, xy;

  /*scll(t);
  while(t--)
  {
    scll(n);

    }
  */

  scll(n);
  scll(m);
  char arr[n][m+2];
  ll leftmost[n+1]={m+1},rightmost[n+1]={0};
  for(i=n-1;i>=0;i--)
  {
      for(j=0;j<m+2;j++)
      {
      scch(arr[i][j]);
      }
  }

  mx=-1;
  for(i=n-1;i>=0;i--)
  {
      for(j=0;j<m+2;j++)
      {
          if(arr[i][j]=='1')
            {
                rightmost[i]=j;
                 if(mx==-1)
                    mx=i;
            }
      }

      for(j=m+1;j>=0;j--)
      {
          if(arr[i][j]=='1')
            {
                leftmost[i]=j;
            }
      }
  }

  ll ans=inf;

  for(i=0;i<(1LL<<n-1);i++)
  {
      ll cur=0,room=0,floor=0;
       while(floor<=mx)
       {
         if(room==0)
         {
             cur+=rightmost[floor]-room;
             room=rightmost[floor];
         }
         else
         {
             cur+=room-leftmost[floor];
             room=leftmost[floor];
         }

          if(floor==mx)
             break;

       ll stairstype=(i&(1LL<<floor))==0?0:m+1;

         cur+=abs(stairstype-room)+1;
         room=stairstype;
         floor++;
     }

      ans=min(ans,cur);
  }

cout<<ans;

return 0;
}


//By the name of God
/*.....   .
      .   .
  ... . ...
  .   .
  .   .....*/
//In mathematics, an unordered pair or pair set is a set of the form {a, b},
//i.e. a set having two elements a and b with no particular relation between them.
//In contrast, an ordered pair (a, b) has a as its first element and b as its second element.
//So, (a,b)!=(b,a) in ordered pair but it is in case of unordered pair.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define   ll  long long int
#define   dl double
#define   llu unsigned long long int
#define   scll(x) scanf("%lld",&x)
#define   scld(x) scanf("%lf",&x)
#define   scull(x) scanf("%llu",&x)
#define   scch(x) scanf(" %c",&x)
#define   eb(x)   emplace_back(x)
#define   pb(x) push_back(x)
#define   pf(x) push_front(x)
#define   ppb() pop_back()
#define   ppf() pop_front()
#define   Pi acos(-1.0)
#define   inf 1000000000000000000
#define   mod 1000000007
#define   ff first
#define   ss second
#define   mpr make_pair
#define   debug(x) cout<<x<<'\n';
#define   MAX 1000000
#define   eps 1e-8
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//Taking input by EOF
//while(scanf("%lld",&x)!=-1){ }
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
//pair can no te used as index in Unordered_map
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
int main()
{
/*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif*/
  ll a, b, c = 1, d, e, f = 0, g, h, i, j = 0, k, m, n, o, p, s, t, v, w, x = 0, y, z, l, res = 0;
  ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = inf;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2, str3 = "", str4 = "";
  ll ide = 0, idx = 0, Q;
  ll xa, xy;

  /*scll(t);
  while(t--)
  {
    scll(n);

    }
  */

  scll(n);
  scll(m);
  char arr[n][m+2];
  ll leftmost[n+1]={m+1},rightmost[n+1]={0};
  for(i=0;i<n;i++)
  {
      for(j=0;j<m+2;j++)
      {
      scch(arr[i][j]);
      }
  }

  mx=-1;
  for(i=0;i<n;i++)
  {
      for(j=0;j<m+2;j++)
      {
          if(arr[i][j]=='1')
            {
                rightmost[i]=j;
                 if(mx==-1)
                    mx=i;
            }
      }

      for(j=m+1;j>=0;j--)
      {
          if(arr[i][j]=='1')
            {
                leftmost[i]=j;
            }
      }
  }

  ll ans=inf;

  for(i=0;i<(1LL<<n-1);i++)
  {
      ll cur=0,room=0,floor=n-1;
       while(floor>=0)
       {
         if(room==0)
         {
             cur+=rightmost[floor]-room;
             room=rightmost[floor];
         }
         else
         {
             cur+=room-leftmost[floor];
             room=leftmost[floor];
         }

          if(floor==mx)
             break;

       ll stairstype=(i&(1LL<<(n-1-floor)))==0?0:m+1;

         cur+=abs(stairstype-room)+1;
         room=stairstype;
         floor--;
     }

      ans=min(ans,cur);
  }

cout<<ans;

return 0;
}


//By the name of God
/*.....   .
      .   .
  ... . ...
  .   .
  .   .....*/
//In mathematics, an unordered pair or pair set is a set of the form {a, b},
//i.e. a set having two elements a and b with no particular relation between them.
//In contrast, an ordered pair (a, b) has a as its first element and b as its second element.
//So, (a,b)!=(b,a) in ordered pair but it is in case of unordered pair.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define   ll  long long int
#define   dl double
#define   llu unsigned long long int
#define   scll(x) scanf("%lld",&x)
#define   scld(x) scanf("%lf",&x)
#define   scull(x) scanf("%llu",&x)
#define   scch(x) scanf(" %c",&x)
#define   eb(x)   emplace_back(x)
#define   pb(x) push_back(x)
#define   pf(x) push_front(x)
#define   ppb() pop_back()
#define   ppf() pop_front()
#define   Pi acos(-1.0)
#define   inf 1000000000000000000
#define   mod 1000000007
#define   ff first
#define   ss second
#define   mpr make_pair
#define   debug(x) cout<<x<<'\n';
#define   MAX 1000000
#define   eps 1e-8
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//Taking input by EOF
//while(scanf("%lld",&x)!=-1){ }
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
//pair can no te used as index in Unordered_map
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
int main()
{
/*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif*/
  ll a, b, c = 1, d, e, f = 0, g, h, i, j = 0, k, m, n, o, p, s, t, v, w, x = 0, y, z, l, res = 0;
  ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = inf;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2, str3 = "", str4 = "";
  ll ide = 0, idx = 0, Q;
  ll xa, xy;

  /*scll(t);
  while(t--)
  {
    scll(n);

    }
  */

  scll(n);
  scll(m);
  char arr[n][m+2];
  ll leftmost[n+1],rightmost[n+1];

  for(i=0;i<n+1;i++)
  {
      leftmost[i]=m+1;
      rightmost[i]=0;
  }
  for(i=0;i<n;i++)
  {
      for(j=0;j<m+2;j++)
      {
      scch(arr[i][j]);
      }
  }

  mx=-1;
  for(i=0;i<n;i++)
  {
      for(j=0;j<m+2;j++)
      {
          if(arr[i][j]=='1')
            {
                rightmost[i]=j;
                 if(mx==-1)
                    mx=i;
            }
      }

      for(j=m+1;j>=0;j--)
      {
          if(arr[i][j]=='1')
            {
                leftmost[i]=j;
            }
      }
  }

  ll ans=inf;

  for(i=0;i<(1LL<<n-1);i++)
  {
      ll cur=0,room=0,floor=n-1;
       while(floor>=0)
       {
         if(room==0)
         {
             cur+=rightmost[floor]-room;
             room=rightmost[floor];
         }
         else
         {
             cur+=room-leftmost[floor];
             room=leftmost[floor];
         }

          if(floor==mx)
             break;

       ll stairstype=(i&(1LL<<(n-1-floor)))==0?0:m+1;

         cur+=abs(stairstype-room)+1;
         room=stairstype;
         floor--;
     }

      ans=min(ans,cur);
  }

cout<<ans;

return 0;
}


//By the name of God
/*.....   .
      .   .
  ... . ...
  .   .
  .   .....*/
//In mathematics, an unordered pair or pair set is a set of the form {a, b},
//i.e. a set having two elements a and b with no particular relation between them.
//In contrast, an ordered pair (a, b) has a as its first element and b as its second element.
//So, (a,b)!=(b,a) in ordered pair but it is in case of unordered pair.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define   ll  long long int
#define   dl double
#define   llu unsigned long long int
#define   scll(x) scanf("%lld",&x)
#define   scld(x) scanf("%lf",&x)
#define   scull(x) scanf("%llu",&x)
#define   scch(x) scanf(" %c",&x)
#define   eb(x)   emplace_back(x)
#define   pb(x) push_back(x)
#define   pf(x) push_front(x)
#define   ppb() pop_back()
#define   ppf() pop_front()
#define   Pi acos(-1.0)
#define   inf 1000000000000000000
#define   mod 1000000007
#define   ff first
#define   ss second
#define   mpr make_pair
#define   debug(x) cout<<x<<'\n';
#define   MAX 1000000
#define   eps 1e-8
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//Taking input by EOF
//while(scanf("%lld",&x)!=-1){ }
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
//pair can no te used as index in Unordered_map
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
int main()
{
/*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif*/
  ll a, b, c = 1, d, e, f = 0, g, h, i, j = 0, k, m, n, o, p, s, t, v, w, x = 0, y, z, l, res = 0;
  ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = inf;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2, str3 = "", str4 = "";
  ll ide = 0, idx = 0, Q;
  ll xa, xy;

  /*scll(t);
  while(t--)
  {
    scll(n);

    }
  */

  scll(n);
  scll(m);
  char arr[n][m+2];
  ll leftmost[n+1],rightmost[n+1];

  for(i=0;i<n;i++)
  {
      leftmost[i]=m+1;
      rightmost[i]=0;
  }
  for(i=n-1;i>=0;i--)
  {
      for(j=0;j<m+2;j++)
      {
      scch(arr[i][j]);
      }
  }

  mx=-1;
  for(i=n-1;i>=0;i--)
  {
      for(j=0;j<m+2;j++)
      {
          if(arr[i][j]=='1')
            {
                rightmost[i]=j;
                 if(mx==-1)
                    mx=i;
            }
      }

      for(j=m+1;j>=0;j--)
      {
          if(arr[i][j]=='1')
            {
                leftmost[i]=j;
            }
      }
  }

  ll ans=inf;

  for(i=0;i<(1LL<<n-1);i++)
  {
      ll cur=0,room=0,floor=0;
       while(floor<=mx)
       {
         if(room==0)
         {
             cur+=rightmost[floor]-room;
             room=rightmost[floor];
         }
         else
         {
             cur+=room-leftmost[floor];
             room=leftmost[floor];
         }

          if(floor==mx)
             break;

       ll stairstype=(i&(1LL<<floor))==0?0:m+1;

         cur+=abs(stairstype-room)+1;
         room=stairstype;
         floor++;
     }

      ans=min(ans,cur);
  }

cout<<ans;

return 0;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;
int n, m ;
cin >> n >> m ;
vector<int> left(n,0) ; // start from left
vector<int> right(n,1e5) ; // start from right
vector<string> h(n) ;
vector<int> dp(n,1e5) ;
for(int i=0;i<n;i++)
{
    cin >> h[n-1-i];
}
int lastl = 0 ;
for(int i=0;i<m+2;i++)
{
    if(h[0][i]=='1')
    {
        lastl = i ;
    }
}
left[0] = lastl ;
int lastr = 1e5 ;
dp[0] = lastl ;
cout << dp[0] << "\n" ;

for(int i=1;i<n;i++)
{
    int nlastl = 0 ;
    int nlastr = m+1 ;
    for(int j=0;j<m+2;j++)
    {
        if(h[i][j]=='1')
        {
            nlastl = j ;
        }
    }
    for(int j=m+1;j>=0;j--)
    {
        if(h[i][j]=='1')
        {
            nlastr = j ;
        }
    }
    left[i] = 1 + nlastl + min(left[i-1] + lastl,right[i-1] + lastr) ;
    right[i] = 1 + (m+1 -nlastr) + min(left[i-1]+m+1 - lastl ,right[i-1] + m+1 - lastr) ;
    lastl = nlastl ;
    lastr = nlastr ;
    dp[i] = min(left[i],right[i]) ;
}
cout << dp[n-1] << "\n" ;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;
int n, m ;
cin >> n >> m ;
vector<int> left(n,0) ; // start from left
vector<int> right(n,1e5) ; // start from right
vector<string> h(n) ;
vector<int> dp(n,1e5) ;
for(int i=0;i<n;i++)
{
    cin >> h[n-1-i];
}
bool flag = true ;
while(n >0 && flag)
{
    for(int i=0;i<m+2;i++)
    {
        if(h[n-1][i]=='1')
        {
            flag = false;
        }
    }
    if(flag)
    {
        n-- ;
    }
}
if(n==0)
{
    cout << "0" << "\n" ;
}
else
{
int lastl = 0 ;
for(int i=0;i<m+2;i++)
{
    if(h[0][i]=='1')
    {
        lastl = i ;
    }
}
left[0] = lastl ;
int lastr = 1e5 ;
dp[0] = lastl ;

for(int i=1;i<n;i++)
{
    int nlastl = 0 ;
    int nlastr = m+1 ;
    for(int j=0;j<m+2;j++)
    {
        if(h[i][j]=='1')
        {
            nlastl = j ;
        }
    }
    for(int j=m+1;j>=0;j--)
    {
        if(h[i][j]=='1')
        {
            nlastr = j ;
        }
    }
    left[i] = 1 + nlastl + min(left[i-1] + lastl,right[i-1] + lastr) ;
    right[i] = 1 + (m+1 -nlastr) + min(left[i-1]+m+1 - lastl ,right[i-1] + m+1 - lastr) ;
    lastl = nlastl ;
    lastr = nlastr ;
    dp[i] = min(left[i],right[i]) ;
}
cout << dp[n-1] << "\n" ;
}
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;
int n, m ;
cin >> n >> m ;
vector<int> left(n,0) ; // start from left
vector<int> right(n,1e5) ; // start from right
vector<string> h(n) ;
vector<int> dp(n,1e5) ;
for(int i=0;i<n;i++)
{
    cin >> h[n-1-i];
}
int lastl = 0 ;
for(int i=0;i<m+2;i++)
{
    if(h[0][i]=='1')
    {
        lastl = i ;
    }
}
left[0] = lastl ;
int lastr = 1e5 ;
dp[0] = lastl ;
for(int i=1;i<n;i++)
{
    int nlastl = 0 ;
    int nlastr = m+1 ;
    for(int j=0;j<m+2;j++)
    {
        if(h[i][j]=='1')
        {
            nlastl = j ;
        }
    }
    for(int j=m+1;j>=0;j--)
    {
        if(h[i][j]=='1')
        {
            nlastr = j ;
        }
    }
    left[i] = 1 + nlastl + min(left[i-1] + lastl,right[i-1] + lastr) ;
    right[i] = 1 + (m+1 -nlastr) + min(left[i-1]+m+1 - lastl ,right[i-1] + m+1 - lastr) ;
    lastl = nlastl ;
    lastr = nlastr ;
    dp[i] = min(left[i],right[i]) ;
}
cout << dp[n-1] << "\n" ;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;
int n, m ;
cin >> n >> m ;
vector<int> left(n,0) ; // start from left
vector<int> right(n,1e5) ; // start from right
vector<string> h(n) ;
vector<int> dp(n,1e5) ;
for(int i=0;i<n;i++)
{
    cin >> h[n-1-i];
}
int lastl = 0 ;
for(int i=0;i<m+2;i++)
{
    if(h[0][i]=='1')
    {
        lastl = i ;
    }
}
left[0] = lastl ;
int lastr = 1e5 ;
dp[0] = lastl ;
cout << dp[0] << "\n" ;

for(int i=1;i<n;i++)
{
    int nlastl = 0 ;
    int nlastr = m+1 ;
    for(int j=0;j<m+2;j++)
    {
        if(h[i][j]=='1')
        {
            nlastl = j ;
        }
    }
    for(int j=m+1;j>=0;j--)
    {
        if(h[i][j]=='1')
        {
            nlastr = j ;
        }
    }
    left[i] = 1 + nlastl + min(left[i-1] + lastl,right[i-1] + lastr) ;
    right[i] = 1 + (m+1 -nlastr) + min(left[i-1]+m+1 - lastl ,right[i-1] + m+1 - lastr) ;
    lastl = nlastl ;
    lastr = nlastr ;
    dp[i] = min(left[i],right[i]) ;
    cout << dp[i] << "\n" ;
}
//cout << dp[n-1] << "\n" ;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;
int n, m ;
cin >> n >> m ;
vector<int> left(n,0) ; // start from left
vector<int> right(n,1e5) ; // start from right
vector<string> h(n) ;
vector<int> dp(n,1e5) ;
for(int i=0;i<n;i++)
{
    cin >> h[n-1-i];
}
int lastl = 0 ;
for(int i=0;i<m+2;i++)
{
    if(h[0][i]=='1')
    {
        lastl = i ;
    }
}
left[0] = lastl ;
int lastr = 1e5 ;
dp[0] = lastl ;

bool flag = true ;
for(int i=0;i<m+2;i++)
{
    if(h[n-1][i]=='1')
    {
        flag = false;
    }
}
if(flag)
{
    n-- ;
}
for(int i=1;i<n;i++)
{
    int nlastl = 0 ;
    int nlastr = m+1 ;
    for(int j=0;j<m+2;j++)
    {
        if(h[i][j]=='1')
        {
            nlastl = j ;
        }
    }
    for(int j=m+1;j>=0;j--)
    {
        if(h[i][j]=='1')
        {
            nlastr = j ;
        }
    }
    left[i] = 1 + nlastl + min(left[i-1] + lastl,right[i-1] + lastr) ;
    right[i] = 1 + (m+1 -nlastr) + min(left[i-1]+m+1 - lastl ,right[i-1] + m+1 - lastr) ;
    lastl = nlastl ;
    lastr = nlastr ;
    dp[i] = min(left[i],right[i]) ;
}
cout << dp[n-1] << "\n" ;
}


#include <iostream>

using namespace std;

string f[200];

int main()
{
    int n, m, r, ans = 0, p;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> f[i];
    for (int j = 1; j <= m; j++)
        if (f[n][j] == '1')
            p = j;
    ans += p;
    //cout << ans << "\n";
    for (int i = n; i >= 2; i--)
    {
        int ln = 1e9, rn = 0;
        for (int j = 1; j <= m; j++)
            if (f[i - 1][j] == '1')
            {
                ln = min(ln, j);
                rn = max(rn, j);
            }
        //cout << ln << " " << rn << "\n";
        int tol = m + 1 - p + 1 + m + 1 - ln;
        int tor = p + 1 + rn;
        if (tol < tor)
        {
            ans += tol;
            p = ln;
        }
        else
        {
            ans += tor;
            p = rn;
        }
        //cout << ans << "\n";
    }
    cout << ans;
    return 0;
}


#include <iostream>

using namespace std;

string f[200];

int main()
{
    int n, m, r, ans = 0, p = 0, em = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> f[i];
    for (int j = 1; j <= m; j++)
        if (f[n][j] == '1')
            p = j;
    ans += p;
    //cout << ans << "\n";
    for (int i = n; i >= 2; i--)
    {
        int ln = 1e9, rn = 0;
        for (int j = 1; j <= m; j++)
            if (f[i - 1][j] == '1')
            {
                ln = min(ln, j);
                rn = max(rn, j);
            }
        //cout << ln << " " << rn << "\n";
        if (ln == 1e9)
            em = i - 1 + min(p, m + 1 - p);
        else if (em != 0)
            em = 0;
        int tol = m + 1 - p + 1 + m + 1 - ((ln == 1e9) ? m + 1 : ln);
        int tor = p + 1 + rn;
        if (tol < tor)
        {
            ans += tol;
            p = ln;
        }
        else
        {
            ans += tor;
            p = rn;
        }
        //cout << ans << "\n";
    }
    cout << ans - em;
    return 0;
}
/* 4 2
0000
0110
0110
0000 */


#include <iostream>

using namespace std;

string f[200];

int main()
{
    int n, m, r, ans = 0, p, em = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> f[i];
    for (int j = 1; j <= m; j++)
        if (f[n][j] == '1')
            p = j;
    ans += p;
    //cout << ans << "\n";
    for (int i = n; i >= 2; i--)
    {
        int ln = 1e9, rn = 0;
        for (int j = 1; j <= m; j++)
            if (f[i - 1][j] == '1')
            {
                ln = min(ln, j);
                rn = max(rn, j);
            }
        //cout << ln << " " << rn << "\n";
        if (ln == 1e9)
            em = i - 1 + min(p, m + 1 - p);
        else if (em != 0)
            em = 0;
        int tol = m + 1 - p + 1 + m + 1 - ((ln == 1e9) ? m + 1 : ln);
        int tor = p + 1 + rn;
        if (tol < tor)
        {
            ans += tol;
            p = ln;
        }
        else
        {
            ans += tor;
            p = rn;
        }
        //cout << ans << "\n";
    }
    cout << ans - em;
    return 0;
}


#include <iostream>

using namespace std;

int n, m, Ans = 1e9, lp[200], rp[200], up = 0;
string f[100];

void BF(int i, int p, int ans)
{
    //cout << i + 1 << " " << ans << "\n";
    if (i < 1)
    {
        //cout << ans << "\n";
        Ans = min(ans, Ans);
        return;
    }
    int st = 0;
    if (i > up)
    {
        st = 1;
        int tor, tol, p1, p2;
        if (lp[i] == 0)
        {
            tor = p + st;
            p1 = 0;
        }
        else
        {
            tor = p + st + rp[i];
            p1 = rp[i];
        }
        BF (i - 1, p1, ans + tor);
        if (lp[i] == 0)
        {
            tol = m + 1 - p + st;
            p2 = m + 1;
        }
        else
        {
            tol = m + 1 - p + st + m + 1 - lp[i];
            p2 = lp[i];
        }
        BF (i - 1, p2, ans + tol);
    }
    else
        BF (0, p, ans);
}

int main()
{
    int yes = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> f[i];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            if (f[i][j] == '1')
            {
                if (lp[i] == 0)
                    lp[i] = j;
                rp[i] = j;
            }
        if (lp[i] != 0)
            yes = 1;
        if (lp[i] == 0 && yes == 0)
            up = i;
    }
    BF(n - 1, rp[n], rp[n]);
    cout << Ans;
    return 0;
}


/* created   : 2020-09-06 09:20:36
 * accepted  : 2020-09-06 09:51:42
 */
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long

void solve() {
  int N, M; cin >> N >> M;
  M += 2;
  vector<string> A(N);
  for (auto& a : A) cin >> a;
  reverse(all(A));
  vector<int> left_most(N, M - 1);
  vector<int> right_most(N);
  int max_floor = -1;
  for (int i = 0; i < N; i++) {
    for (int j = M - 1; j >= 0; j--) {
      if (A[i][j] == '1') {
        left_most[i] = j;
        max_floor = i;
      }
    }
    for (int j = 0; j < M; j++) {
      if (A[i][j] == '1') {
        right_most[i] = j;
      }
    }
  }
  int ans = 1e9;
  for (int state = 0; state < (1 << N); state++) {
    int floor = 0, cur = 0, room = 0;
    while (floor <= max_floor) {
      if (room == 0) {
        cur += right_most[floor] - room;
        room = right_most[floor];
      }
      else {
        cur += room - left_most[floor];
        room = left_most[floor];
      }
      if (floor == max_floor) break;
      int nxt = (state & (1 << floor)) == 0 ? 0 : M - 1;
      cur += abs(nxt - room) + 1;
      room = nxt;
      floor++;
    }
    ans = min(ans, cur);
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}


/* created   : 2020-09-06 09:20:36
 * accepted  : 2020-09-06 09:48:18
 */
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long

void solve() {
  int N, M; cin >> N >> M;
  M += 2;
  vector<string> A(N);
  for (auto& a : A) cin >> a;
  reverse(all(A));
  vector<int> left_most(N);
  vector<int> right_most(N);
  int max_floor = -1;
  for (int i = 0; i < N; i++) {
    for (int j = M - 1; j >= 0; j--) {
      if (A[i][j] == '1') {
        left_most[i] = j;
        max_floor = i;
      }
    }
    for (int j = 0; j < M; j++) {
      if (A[i][j] == '1') {
        right_most[i] = j;
      }
    }
  }
  int ans = 1e9;
  for (int state = 0; state < (1 << N); state++) {
    // if (state & 1) continue; // first floor must start at left.
    int floor = 0, cur = 0, room = 0;
    while (floor <= max_floor) {
      if (room == 0) {
        cur += right_most[floor] - room;
        room = right_most[floor];
      }
      else {
        cur += room - left_most[floor];
        room = left_most[floor];
      }
      if (floor == max_floor) break;
      int nxt = (state & (1 << floor)) == 0 ? 0 : M - 1;
      cur += abs(nxt - room) + 1;
      room = nxt;
      floor++;
    }
    ans = min(ans, cur);
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}


/* created   : 2020-09-04 07:45:50
 * accepted  : 2020-09-06 09:07:22
 */
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long

void amin(int& a, int b) { a = min(a, b); }

const int NX = 20;
int f[NX][2];
vector<string> A;
int N, M;

int rec(int x, int y) {
  int& ans = f[x][y];
  if (ans >= 0) return ans;
  bool ok = true;
  for (int i = x + 1; i < N; i++) {
    if (A[i] != string(M, '0')) {
      ok = false;
      break;
    }
  }
  if (ok) {
    // If after accessing row x, we are done.
    if (y == 0) {
      // we are at the left upstairs.
      int i = M - 1;
      while (i >= 0 && A[x][i] == '0') i--;
      ans = max(i, 0);
    }
    else {
      int i = 0;
      while (i < M && A[x][i] == '0') i++;
      ans = max(M - 1 - i, 0);
    }
    return ans;
  }
  ans = 1e9;
  // 1). go to the other side upstairs, and go up to the next floor
  amin(ans, rec(x + 1, y ^ 1) + M);
  if (y == 0) {
  // 2). current at left side, also go up to next level from this side.
    int i = M - 1;
    while (i > 0 && A[x][i] == '0') i--;
    amin(ans, 2 * i + 1 + rec(x + 1, y));
  }
  else {
  // 3). current at right side, also go up to next level from this side.
    int i = 0;
    while (i < M - 1 && A[x][i] == '0') i++;
    amin(ans, 2 * (M - 1 - i) + 1 + rec(x + 1, y));
  }
  return ans;
}

void solve() {
  cin >> N >> M;
  M += 2;
  A.resize(N);
  for (auto& s : A) {
    cin >> s;
  }
  reverse(all(A));
  memset(f, 0xff, sizeof(f));
  cout << rec(0, 0) << "\n";
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}


/* created   : 2020-09-04 07:45:50
 * accepted  : 2020-09-06 08:53:53
 */
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long

void amin(int& a, int b) { a = min(a, b); }

const int NX = 20;
int f[NX][2];
vector<string> A;
int N, M;

int rec(int x, int y) {
  int& ans = f[x][y];
  if (ans >= 0) return ans;
  bool ok = true;
  for (int i = x + 1; i < N; i++) {
    if (A[i] != string(M, '0')) {
      ok = false;
      break;
    }
  }
  if (ok) {
    // If after accessing row x, we are done.
    if (y == 0) {
      // we are at the left upstairs.
      int i = M - 1;
      while (i >= 0 && A[x][i] == '0') i--;
      ans = max(i, 0);
    }
    else {
      int i = 0;
      while (i < M && A[x][i] == '0') i++;
      ans = max(M - 1 - i, 0);
    }
    return ans;
  }
  ans = 1e9;
  // 1). go to the other side upstairs, and go up to next floor
  amin(ans, rec(x + 1, y ^ 1) + M);
  if (y == 0) {
  // 2). current at left side, also go up to next level from this side.
    int i = M - 1;
    while (i >= 0 && A[x][i] == '0') i--;
    amin(ans, 2 * i + 1 + rec(x + 1, y));
  }
  else {
  // 3). current at right side, also go up to next level from this side.
    int i = 0;
    while (i < M && A[x][i] == '0') i++;
    amin(ans, 2 * (M - 1 - i) + 1 + rec(x + 1, y));
  }
  return ans;
}

void solve() {
  cin >> N >> M;
  M += 2;
  A.resize(N);
  for (auto& s : A) {
    cin >> s;
  }
  reverse(all(A));
  memset(f, 0xff, sizeof(f));
  cout << rec(0, 0) << "\n";
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=max(0,a[0].second);
    dp[0][1]=max(0,a[0].second);
    //a[0].first = a[0].second;
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first + a[i].second < 0)
        {
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1-skip][0] + 2*f - 1 - a[i-1-skip].first - a[i].first;
        b= dp[i-1-skip][1] + 2*f - 1 - a[i-1-skip].second -a[i].first;
        dp[i][0]=min(w,b)+skip;

        w=dp[i-1-skip][0] + 1 + a[i-1-skip].first +a[i].second;
        b=dp[i-1-skip][1] + 1 + a[i-1-skip].second +a[i].second;
        dp[i][1]=min(w,b)+skip;
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return -1;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return -1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
        a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first == 0)
        {
            dp[i][0]=min( dp[i-1][0] + a[i-1].first +1 ,
                          dp[i-1][1] + a[i-1].second +1 );
            dp[i][1]=min( dp[i-1][0] + f-1 - a[i-1].first + 1,
                          dp[i-1][1] + f-1 - a[i-1].second + 1 );
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1][0] + 2*f - 1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f - 1 - a[i-1].second -a[i].first;
        dp[i][0]=min(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].second;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].second;
        //cout<<w<<' '<<a[i-1].second<<endl;
        dp[i][1]=min(w,b);
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return 0;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return s.length()-1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i==0 and g==0)
        a[i]={0,0};
        else if(i==0 and g>0)
        a[i]={l,l};
        else
        a[i]={g,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=max(0,a[0].second);
    dp[0][1]=max(0,a[0].second);
    //a[0].first = a[0].second;
    if( a[0].first + a[0].second < 0)
        {
            skip++;
        }
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first + a[i].second < 0)
        {
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1-skip][0] + 2*f - 1 - a[i-1-skip].first - a[i].first;
        b= dp[i-1-skip][1] + 2*f - 1 - a[i-1-skip].second -a[i].first;
        dp[i][0]=min(w,b)+skip;

        w=dp[i-1-skip][0] + 1 + a[i-1-skip].first +a[i].second;
        b=dp[i-1-skip][1] + 1 + a[i-1-skip].second +a[i].second;
        dp[i][1]=min(w,b)+skip;
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return -1;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return -1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
        a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[16][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    for (int i=1;i<=e;i++)
    {
        int w,b;
        w= dp[i-1][0] + 2*f +1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f +1 - a[i-1].second -a[i].first;
        dp[i][0]=max(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].first;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].first;
        dp[i][1]=max(w,b);
    }
    return min(dp[e][0],dp[e][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e+1);
    for (int j=1;j<=e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int f,l;
        f=findf(s);
        l=findr(s);
        a[i]={f,l};
    }
    int ans=stairs(e-1,f-1);
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    //a[0].first = a[0].second;
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first + a[i].second < 0)
        {
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1-skip][0] + 2*f - 1 - a[i-1-skip].first - a[i].first;
        b= dp[i-1-skip][1] + 2*f - 1 - a[i-1-skip].second -a[i].first;
        dp[i][0]=min(w,b)+skip;

        w=dp[i-1-skip][0] + 1 + a[i-1-skip].first +a[i].second;
        b=dp[i-1-skip][1] + 1 + a[i-1-skip].second +a[i].second;
        dp[i][1]=min(w,b)+skip;
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return -1;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return -1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
        a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=max(0,a[0].second);
    dp[0][1]=max(0,a[0].second);
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first == 0)
        {
            dp[i][0]=min( dp[i-1][0] + a[i-1].first +1 ,
                          dp[i-1][1] + a[i-1].second +1 );
            dp[i][1]=min( dp[i-1][0] + f - a[i-1].first + 1,
                          dp[i-1][1] + f - a[i-1].second + 1 );
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1][0] + 2*f - 1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f - 1 - a[i-1].second -a[i].first;
        dp[i][0]=min(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].second;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].second;
        dp[i][1]=min(w,b);
        skip=0;
        cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return 0;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return s.length()-1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i==0 and g==0)
        a[i]={0,0};
        else
        a[i]={g,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    //a[0].first = a[0].second;
    for (int i=1;i<e;i++)
    {
        int w,b;
        w= dp[i-1][0] + 2*f - 1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f - 1 - a[i-1].second -a[i].first;
        dp[i][0]=min(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].second;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].second;
        dp[i][1]=min(w,b);
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1][0],dp[e-1][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
            a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first == 0)
        {
            dp[i][0]=min( dp[i-1][0] + a[i-1].first +1 ,
                          dp[i-1][1] + a[i-1].second +1 );
            dp[i][1]=min( dp[i-1][0] + f-1 - a[i-1].first + 1,
                          dp[i-1][1] + f-1 - a[i-1].second + 1 );
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1][0] + 2*f - 1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f - 1 - a[i-1].second -a[i].first;
        dp[i][0]=min(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].second;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].second;
        dp[i][1]=min(w,b);
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return 0;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return s.length()-1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i==0 and g==0)
        a[i]={0,0};
        else
        a[i]={g,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    //a[0].first = a[0].second;
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first + a[i].second < 0)
        {
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1-skip][0] + 2*f - 1 - a[i-1-skip].first - a[i].first;
        b= dp[i-1-skip][1] + 2*f - 1 - a[i-1-skip].second -a[i].first;
        dp[i][0]=min(w,b)+skip;

        w=dp[i-1-skip][0] + 1 + a[i-1-skip].first +a[i].second;
        b=dp[i-1-skip][1] + 1 + a[i-1-skip].second +a[i].second;
        dp[i][1]=min(w,b)+skip;
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return -1;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return -1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
        a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=max(0,a[0].second);
    dp[0][1]=max(0,a[0].second);
    //a[0].first = a[0].second;
    int skip=0;
    if( a[0].first + a[0].second < 0)
        {
            skip++;
        }
    for (int i=1;i<e;i++)
    {
        if( a[i].first + a[i].second < 0)
        {
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1-skip][0] + 2*f - 1 - a[i-1-skip].first - a[i].first;
        b= dp[i-1-skip][1] + 2*f - 1 - a[i-1-skip].second -a[i].first;
        dp[i][0]=min(w,b)+skip;

        w=dp[i-1-skip][0] + 1 + a[i-1-skip].first +a[i].second;
        b=dp[i-1-skip][1] + 1 + a[i-1-skip].second +a[i].second;
        dp[i][1]=min(w,b)+skip;
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return -1;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return -1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i!=0)
        a[i]={g,l};
        else
        a[i]={l,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[15][2];
int stairs(int e,int f)
{
    dp[0][0]=max(0,a[0].second);
    dp[0][1]=max(0,a[0].second);
    int skip=0;
    for (int i=1;i<e;i++)
    {
        if( a[i].first == 0)
        {
            dp[i][0]=min( dp[i-1][0] + a[i-1].first +1 ,
                          dp[i-1][1] + a[i-1].second +1 );
            dp[i][1]=min( dp[i-1][0] + f - a[i-1].first + 1,
                          dp[i-1][1] + f - a[i-1].second + 1 );
            skip++;
            continue;
        }
        int w,b;
        w= dp[i-1][0] + 2*f - 1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f - 1 - a[i-1].second -a[i].first;
        dp[i][0]=min(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].second;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].second;
        dp[i][1]=min(w,b);
        skip=0;
        //cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1]<<endl;
    }
    return min(dp[e-1-skip][0],dp[e-1-skip][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
    return 0;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
    return s.length()-1;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e);
    for (int j=0;j<e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int g,l;
        g=findf(s);
        l=findr(s);
        if(i==0 and g==0)
        a[i]={0,0};
        else
        a[i]={g,l};
    }
    int ans=stairs(e,f+2);
    cout<<max(ans,0)<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define pi pair<int,int>
#define ll long long
using namespace std;
vector<pi> a;
int dp[16][2];
int stairs(int e,int f)
{
    dp[0][0]=a[0].second;
    dp[0][1]=a[0].second;
    for (int i=1;i<=e;i++)
    {
        int w,b;
        w= dp[i-1][0] + 2*f +1 - a[i-1].first - a[i].first;
        b= dp[i-1][1] + 2*f +1 - a[i-1].second -a[i].first;
        dp[i][0]=max(w,b);

        w=dp[i-1][0] + 1 + a[i-1].first +a[i].first;
        b=dp[i-1][1] + 1 + a[i-1].second +a[i].first;
        dp[i][1]=max(w,b);
    }
    return max(dp[e][0],dp[e][1]);
}
int findf(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]=='1')
        return i;
}
int findr(string s)
{
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='1')
        return i;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e,f;
    cin>>e>>f;
    a.resize(e+1);
    for (int j=1;j<=e;j++)
    {
        dp[j][0]=-1;
        dp[j][1]=-1;
    }
    for(int i=e-1;i>=0;i--)
    {
        string s;
        cin>>s;
        int f,l;
        f=findf(s);
        l=findr(s);
        a[i]={f,l};
    }
    int ans=stairs(e-1,f-1);
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define int long long int
#define mp make_pair
#define pb push_back

vector <int> arr,brr;
int m;

int dp[3][105];

int ans(int pos,int i)
{
     if(i+1==arr.size())
     {
          if(pos==0)
          return arr[i];
          else
          return brr[i];
     }

     if(dp[pos][i]!=-1)
     return dp[pos][i];

     if(pos==0)
     return dp[pos][i]=min(arr[i]+ans(0,i+1),m+2+ans(1,i+1));
     else
     return dp[pos][i]=min(m+2+ans(0,i+1),brr[i]+ans(1,i+1));
}

int32_t main()
{

     memset(dp,-1,sizeof(dp));

     int n;
     cin>>n>>m;

     vector <string> v;

     int flag=0;

     for(int i=1;i<=n;i++)
     {
         string str;
         cin>>str;

         for(int j=0;j<str.size();j++)
         {
              if(str[j]=='1')
              flag=1;
         }

         if(flag==1)
         v.pb(str);
     }

     reverse(v.begin(),v.end());

     for(int i=0;i+1<v.size();i++)
     {
          int idx=-1,id=1000;

          for(int j=0;j<v[i].size();j++)
          {
               if(v[i][j]=='1')
               {
                    idx=max(idx,j);
                    id=min(id,j);
               }
          }

          if(idx==-1)
          arr.pb(1);
          else
          arr.pb(2*idx+1);
          if(id==-1)
          brr.pb(1);
          brr.pb(2*(m+1-id)+1);
     }

     if(v.size()>0)
     {
          string st=v[v.size()-1];

          int idx=-1,id=1000;

          for(int i=0;i<st.size();i++)
          {
               if(st[i]=='1')
               {
                    idx=max(idx,i);
                    id=min(id,i);
               }
          }

          arr.pb(idx);
          brr.pb(m+1-id);
     }

     if(arr.size()==0)
     {
          cout<<"0";
          return 0;
     }

     cout<<ans(0,0);
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define int long long int
#define mp make_pair
#define pb push_back

vector <int> arr,brr;
int m;

int dp[3][105];

int ans(int pos,int i)
{
     if(i+1==arr.size())
     {
          if(pos==0)
          return arr[i];
          else
          return brr[i];
     }

     //if(dp[pos][i]!=-1)
     //return dp[pos][i];

     if(pos==0)
     return min(arr[i]+ans(0,i+1),m+2+ans(1,i+1));
     else
     return min(m+2+ans(0,i+1),brr[i]+ans(1,i+1));
}

int32_t main()
{

     memset(dp,-1,sizeof(dp));

     int n;
     cin>>n>>m;

     vector <string> v;

     int flag=0;

     for(int i=1;i<=n;i++)
     {
         string str;
         cin>>str;

         for(int j=0;j<str.size();j++)
         {
              if(str[j]=='1')
              flag=1;
         }

         if(flag==1)
         v.pb(str);
     }

     reverse(v.begin(),v.end());

     for(int i=0;i+1<v.size();i++)
     {
          int idx=-1,id=1000;

          for(int j=0;j<v[i].size();j++)
          {
               if(v[i][j]=='1')
               {
                    idx=max(idx,j);
                    id=min(id,j);
               }
          }

          if(idx==-1)
          arr.pb(1);
          else
          arr.pb(2*idx+1);
          if(id==1000)
          brr.pb(1);
          else
          brr.pb(2*(m+1-id)+1);
     }

     if(v.size()>0)
     {
          string st=v[v.size()-1];

          int idx=-1,id=1000;

          for(int i=0;i<st.size();i++)
          {
               if(st[i]=='1')
               {
                    idx=max(idx,i);
                    id=min(id,i);
               }
          }

          arr.pb(idx);
          brr.pb(m+1-id);
     }

     if(arr.size()==0)
     {
          cout<<"0";
          return 0;
     }

     cout<<ans(0,0);
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define int long long int
#define mp make_pair
#define pb push_back

vector <int> arr,brr;
int m;

int dp[3][105];

int ans(int pos,int i)
{
     if(i+1==arr.size())
     {
          if(pos==0)
          return arr[i];
          else
          return brr[i];
     }

     if(dp[pos][i]!=-1)
     return dp[pos][i];

     if(pos==0)
     return dp[pos][i]=min(arr[i]+ans(0,i+1),m+2+ans(1,i+1));
     else
     return dp[pos][i]=min(m+2+ans(0,i+1),brr[i]+ans(1,i+1));
}

int32_t main()
{

     memset(dp,-1,sizeof(dp));

     int n;
     cin>>n>>m;

     vector <string> v;

     int flag=0;

     for(int i=1;i<=n;i++)
     {
         string str;
         cin>>str;

         for(int j=0;j<str.size();j++)
         {
              if(str[j]=='1')
              flag=1;
         }

         if(flag==1)
         v.pb(str);
     }

     reverse(v.begin(),v.end());

     for(int i=0;i<v.size()-1;i++)
     {
          int idx=-1,id=1000;

          for(int j=0;j<v[i].size();j++)
          {
               if(v[i][j]=='1')
               {
                    idx=max(idx,j);
                    id=min(id,j);
               }
          }

          if(idx==-1)
          arr.pb(1);
          else
          arr.pb(2*idx+1);
          if(id==-1)
          brr.pb(1);
          brr.pb(2*(m+1-id)+1);
     }

     if(v.size()>0)
     {
          string st=v[v.size()-1];

          int idx=-1,id=1000;

          for(int i=0;i<st.size();i++)
          {
               if(st[i]=='1')
               {
                    idx=max(idx,i);
                    id=min(id,i);
               }
          }

          arr.pb(idx);
          brr.pb(m+1-id);
     }

     if(arr.size()==0)
     {
          cout<<"0";
          return 0;
     }

     cout<<ans(0,0);
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define int long long int
#define mp make_pair
#define pb push_back

vector <int> arr,brr;
int m;

int dp[3][105];

int ans(int pos,int i)
{
     if(i+1==arr.size())
     {
          if(pos==0)
          return arr[i];
          else
          return brr[i];
     }

     if(dp[pos][i]!=-1)
     return dp[pos][i];

     if(pos==0)
     return dp[pos][i]=min(arr[i]+ans(0,i+1),m+2+ans(1,i+1));
     else
     return dp[pos][i]=min(m+2+ans(0,i+1),brr[i]+ans(1,i+1));
}

int32_t main()
{

     memset(dp,-1,sizeof(dp));

     int n;
     cin>>n>>m;

     vector <string> v;

     int flag=0;

     for(int i=1;i<=n;i++)
     {
         string str;
         cin>>str;

         for(int j=0;j<str.size();j++)
         {
              if(str[j]=='1')
              flag=1;
         }

         if(flag==1)
         v.pb(str);
     }

     reverse(v.begin(),v.end());

     for(int i=0;i<v.size()-1;i++)
     {
          int idx=-1,id=1000;

          for(int j=0;j<v[i].size();j++)
          {
               if(v[i][j]=='1')
               {
                    idx=max(idx,j);
                    id=min(id,j);
               }
          }

          if(idx==-1)
          arr.pb(1);
          else
          arr.pb(2*idx+1);
          if(id==-1)
          brr.pb(1);
          brr.pb(2*(m+1-id)+1);
     }

     if(v.size()>0)
     {
          string st=v[v.size()-1];

          int idx=-1,id=1000;

          for(int i=0;i<st.size();i++)
          {
               if(st[i]=='1')
               {
                    idx=max(idx,i);
                    id=min(id,i);
               }
          }

          arr.pb(idx);
          brr.pb(m+1-id);
     }

     if(arr.size()==0)
     {
          cout<<"0";
          return 0;
     }
     if(arr.size()==1)
     {
          cout<<min(arr[0],brr[0]);
          return 0;
     }
     cout<<ans(0,0);
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define int long long int
#define mp make_pair
#define pb push_back

vector <int> arr,brr;
int m;

int dp[3][105];

int ans(int pos,int i)
{
     if(i+1==arr.size())
     {
          if(pos==0)
          return arr[i];
          else
          return brr[i];
     }

     if(dp[pos][i]!=-1)
     return dp[pos][i];

     if(pos==0)
     return dp[pos][i]=min(arr[i]+ans(0,i+1),m+2+ans(1,i+1));
     else
     return dp[pos][i]=min(m+2+ans(0,i+1),brr[i]+ans(1,i+1));
}

int32_t main()
{

     memset(dp,-1,sizeof(dp));

     int n;
     cin>>n>>m;

     vector <string> v;

     int flag=0;

     for(int i=1;i<=n;i++)
     {
         string str;
         cin>>str;

         for(int j=0;j<str.size();j++)
         {
              if(str[j]=='1')
              flag=1;
         }

         if(flag==1)
         v.pb(str);
     }

     reverse(v.begin(),v.end());

     for(int i=0;i+1<v.size();i++)
     {
          int idx=-1,id=1000;

          for(int j=0;j<v[i].size();j++)
          {
               if(v[i][j]=='1')
               {
                    idx=max(idx,j);
                    id=min(id,j);
               }
          }

          if(idx==-1)
          arr.pb(1);
          else
          arr.pb(2*idx+1);
          if(id==1000)
          brr.pb(1);
          else
          brr.pb(2*(m+1-id)+1);
     }

     if(v.size()>0)
     {
          string st=v[v.size()-1];

          int idx=-1,id=1000;

          for(int i=0;i<st.size();i++)
          {
               if(st[i]=='1')
               {
                    idx=max(idx,i);
                    id=min(id,i);
               }
          }

          arr.pb(idx);
          brr.pb(m+1-id);
     }

     if(arr.size()==0)
     {
          cout<<"0";
          return 0;
     }

     cout<<ans(0,0);
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

int pc_0,pc_1;
int t=0;

void rec(int ind){
    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==t){
        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        pc_0=cost_0,pc_1=cost_1;
        cost_0=min(cost_0+2*locs[ind].first,cost_1+m+1)+1;
        cost_1=min(cost_1+2*locs[ind].second,pc_0+m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int flag=0;
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
        if(locs[i].first==0 && flag==0)t++;
        if(locs[i].first!=0)flag++;
    }
    if(n==1 || flag==0 || n-1==t){
        cout<<locs[n-1].first<<"\n";
        return 0;
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(t);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

void rec(int ind){
    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==0){

        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        if(locs[ind].first==0){
            cost_0--;
            if(2*locs[ind+1].first<m+1)cost_0-=locs[ind+1].first;
            else cost_0-=(m+1)+locs[ind].second;
            cost_1--;
            if(2*locs[ind+1].second<m+1)cost_1-=locs[ind+1].second;
            else cost_1-=(m+1)+locs[ind].first;
        }
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        cost_0=cost_0+min(2*locs[ind].first,m+1)+1;
        cost_1=cost_1+min(2*locs[ind].second,m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
    }
    if(n==1){
        cout<<locs[0].first<<"\n";
        return 0;
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(0);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

void rec(int ind){
    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==0){
        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        if(locs[ind].first==0){
            if(ind+1!=n-1){
                cost_0--;
                if(2*locs[ind+1].first<m+1)cost_0-=locs[ind+1].first;
                else cost_0-=(m+1)+locs[ind].second;
                cost_1--;
                if(2*locs[ind+1].second<m+1)cost_1-=locs[ind+1].second;
                else cost_1-=(m+1)+locs[ind].first;
            }
            else{
                cost_0--;
                cost_1--;
                cost_0=locs[ind+1].first;
            }
        }
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        cost_0=cost_0+min(2*locs[ind].first,m+1)+1;
        cost_1=cost_1+min(2*locs[ind].second,m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int flag=0;
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
        if(locs[i].first!=0)flag++;
    }
    if(n==1 || flag==0){
        cout<<locs[0].first<<"\n";
        return 0;
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(0);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

void rec(int ind){

    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==0){

        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        if(locs[ind].first==0){
            cost_0--;
            cost_1--;
        }
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        cost_0=cost_0+min(2*locs[ind].first,m+1)+1;
        cost_1=cost_1+min(2*locs[ind].second,m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(0);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

void rec(int ind){
    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==0){
        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        if(locs[ind].first==0){
            if(ind+1!=n-1){
                cost_0--;
                if(2*locs[ind+1].first<m+1)cost_0-=locs[ind+1].first;
                else cost_0-=(m+1)+locs[ind].second;
                cost_1--;
                if(2*locs[ind+1].second<m+1)cost_1-=locs[ind+1].second;
                else cost_1-=(m+1)+locs[ind].first;
            }
            else{
                cost_0--;
                cost_1--;
                cost_0=locs[ind+1].first;
            }
        }
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        cost_0=cost_0+min(2*locs[ind].first,m+1)+1;
        cost_1=cost_1+min(2*locs[ind].second,m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
    }
    if(n==1){
        cout<<locs[0].first<<"\n";
        return 0;
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(0);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define MAXN 205
#define ll long long int

vector<string> a(15);
int n,m;
int cost_1=0,cost_0=0;
vector<pair<int,int>> locs(15);

void rec(int ind){
    if(ind==n-1){
        cost_0=locs[ind].first+locs[ind].first;
        cost_1=m+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    rec(ind+1);
    if(ind==0){

        cost_0=cost_0+locs[ind].first+1;
        cost_1=cost_1+locs[ind].second+1;
        if(locs[ind].first==0){
            cost_0--;
            if(2*locs[ind+1].first<m+1)cost_0-=locs[ind+1].first;
            else cost_0-=(m+1)+locs[ind].second;
            cost_1--;
            if(2*locs[ind+1].second<m+1)cost_1-=locs[ind+1].second;
            else cost_1-=(m+1)+locs[ind].first;
        }
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
    else{
        cost_0=cost_0+min(2*locs[ind].first,m+1)+1;
        cost_1=cost_1+min(2*locs[ind].second,m+1)+1;
        //cout<<cost_0<<" "<<cost_1<<"\n";
        return;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        for(int j=1;j<m+1;j++){
            if(a[i][j]=='1'){
                locs[i].first=j;
            }
        }
        for(int j=m;j>=1;j--){
            if(a[i][j]=='1'){
                locs[i].second=m+1-j;
            }
        }
    }
    //for(int i=0;i<n;i++)cout<<locs[i].first<<" "<<locs[i].second<<"\n";
    rec(0);
    cout<<min(cost_0,cost_1)<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    int j;
    for(j=i-1;j>=0;j--){
        if(border[j][0])
            break;
    }
    if(j<0)
        return 0;
    if(i==0)
        return 0;
    else{
        int mini=inf;
        if(!prev){
            mini=min(recurse(border,i-1,0)+border[i][1]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,1)+2*(m+1)-border[i][1]-border[i-1][0]+1,mini);
        }
        else{
            mini=min(recurse(border,i-1,0)+border[i][0]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,0)+2*(m+1)-border[i][0]-border[i-1][0]+1,mini);
        }
        return mini;
    }
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,0)+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    int j;
    for(j=i-1;j>=0;j--){
        if(border[j][0])
            break;
    }
    if(j<0)
        return 0;
    int mini=inf;
    if(!border[i-1][0])
        mini=min(mini,recurse(border,i-1,prev)+1);
    else{
        mini=min(mini,recurse(border,i-1,border[i-1][0])+2*(m+1)-prev-border[i-1][0]+1);
        mini=min(mini,recurse(border,i-1,border[i-1][1])+prev+1+border[i-1][1]);
    }
    return mini;
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,border[n-1][1])+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    if(i==0)
        return 0;
    else{
        int mini=inf;
        if(!prev){
            mini=min(recurse(border,i-1,0)+border[i][1]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,1)+2*(m+1)-border[i][1]-border[i-1][0]+1,mini);
        }
        else{
            mini=min(recurse(border,i-1,0)+border[i][0]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,0)+2*(m+1)-border[i][0]-border[i-1][0]+1,mini);
        }
        return mini;
    }
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,0)+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    for(j=i-1;j>=0;j--){
        if(border[j][0])
            break;
    }
    if(j<0)
        return 0;
    if(i==0)
        return 0;
    else{
        int mini=inf;
        if(!prev){
            mini=min(recurse(border,i-1,0)+border[i][1]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,1)+2*(m+1)-border[i][1]-border[i-1][0]+1,mini);
        }
        else{
            mini=min(recurse(border,i-1,0)+border[i][0]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,0)+2*(m+1)-border[i][0]-border[i-1][0]+1,mini);
        }
        return mini;
    }
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,0)+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    if(i>0&&!border[i-1][0]){
        return 0;
    }
    if(i==0)
        return 0;
    else{
        int mini=inf;
        if(!prev){
            mini=min(recurse(border,i-1,0)+border[i][1]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,1)+2*(m+1)-border[i][1]-border[i-1][0]+1,mini);
        }
        else{
            mini=min(recurse(border,i-1,0)+border[i][0]+border[i-1][1]+1,mini);
            mini=min(recurse(border,i-1,0)+2*(m+1)-border[i][0]-border[i-1][0]+1,mini);
        }
        return mini;
    }
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,0)+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(x) cerr << #x << " is " << x << endl;
const int mod=1e9+7;
int dp[16][2],m;
int recurse(int border[][2],int i,int prev){
    int j;
    for(j=i-1;j>=0;j--){
        if(border[j][0])
            break;
    }
    if(j<0)
        return 0;
    int mini=inf;
    if(!border[i-1][0])
        mini=min(mini,recurse(border,i-1,prev)+1);
    else{
        mini=min(mini,recurse(border,i-1,border[i-1][0])+2*(m+1)-border[i][1]-border[i-1][0]+1);
        mini=min(mini,recurse(border,i-1,border[i-1][1])+prev+1+border[i-1][1]);
    }
    return mini;
}
int main(){
IOS;
int n;
cin>>n>>m;
int a[n][m+2];
int i,j;
string s;
for(i=0;i<n;i++){
    cin>>s;
    for(j=0;j<m+2;j++){
        if(s[j]-48)
            a[i][j]=1;
        else a[i][j]=0;
    }
}
int border[n][2];
memset(border,0,sizeof(border));
for(i=n-1;i>=0;i--){
    for(j=0;j<m+2;j++){
        if(a[i][j]){
            if(!border[i][0])
                border[i][0]=j;
            border[i][1]=j;
        }
    }
}
cout<<recurse(border,n-1,border[n-1][1])+border[n-1][1]<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000007
#define inf -100000
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll int n,m;
    cin>>n>>m;
    ll int a[n][m+2];
    ll int i,j;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<m+2;j++)
    	{
    		char x;
    		cin>>x;
    		if(x=='1')
    			a[i][j]=1;
    		else
    			a[i][j]=0;
    		//cout<<a[i][j];
    	}
    //	cout<<endl;
    }

    ll int dp[n][2];////////   0 for left and 1 for right pe wait
    ll int f=0,left,right;
    f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[n-1][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[n-1][i]==1)
    	{
    		right=i;
    	}
    }
    if(n==1)
    {
    	cout<<right<<endl;
    	exit(0);
    }
    dp[n-1][0]=2*right;
    dp[n-1][1]=m+1;
  //  cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
    if(n>=3)
    {
    for(i=n-2;i>=1;i--)
    {
    	f=0;left=m+1,right=0;
    	for(j=0;j<m+2;j++)
    	{
	    	if(f==0 && a[i][j]==1)
	    	{
	    		f=1;
	    		left=j;
	    		right=j;
	    	}
	    	else if(f==1 && a[n-1][j]==1)
	    	{
	    		right=j;
	    	}
    	}

	    dp[i][0]=min(2*right+dp[i+1][0],dp[i+1][1]+m+1)+1;
	    dp[i][1]=min(2*(m+1-left)+dp[i+1][1],dp[i+1][0]+m+1)+1;

    }
	}

	f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[0][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[0][i]==1)
    	{
    		right=i;
    	}
    }
    ll int ans;
    ans=min(dp[1][0]+right,dp[1][1]+(m+1-left))+1;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000007
#define inf -100000
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll int n,m;
    cin>>n>>m;
    ll int a[n][m+2];
    ll int i,j;
    ll int c[n]={0};
    ll int tot=0;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<m+2;j++)
    	{
    		char x;
    		cin>>x;
    		if(x=='1')
    			a[i][j]=1;
    		else
    			a[i][j]=0;
    		if(a[i][j]==1)
    			{
    				c[i]++;
    				tot++;
    			}

    		//cout<<a[i][j];
    	}
    //	cout<<endl;
    }
    ll int dp[n][2];////////   0 for left and 1 for right pe wait
    ll int f=0,left,right;
    f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[n-1][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[n-1][i]==1)
    	{
    		right=i;
    	}
    }
    if(n==1)
    {
    	cout<<right<<endl;
    	exit(0);
    }

    dp[n-1][0]=2*right;
    dp[n-1][1]=m+1;
    tot=tot-c[n-1];

    ll int ans;
    if(tot==0)
    {
    	cout<<right<<endl;exit(0);
    }
  //  cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
    if(n>=3)
    {
    for(i=n-2;i>=1;i--)
    {
    	f=0;left=m+1,right=0;
    	for(j=0;j<m+2;j++)
    	{
	    	if(f==0 && a[i][j]==1)
	    	{
	    		f=1;
	    		left=j;
	    		right=j;
	    	}
	    	else if(f==1 && a[n-1][j]==1)
	    	{
	    		right=j;
	    	}
    	}
    	if(tot-c[i]==0)
    	{
    		ans=min(dp[i+1][0]+right,dp[i+1][1]+(m+1-left))+1;
    		cout<<ans<<endl;exit(0);
    	}
    	else
    		tot=tot-c[i];
	    dp[i][0]=min(2*right+dp[i+1][0],dp[i+1][1]+m+1)+1;
	    dp[i][1]=min(2*(m+1-left)+dp[i+1][1],dp[i+1][0]+m+1)+1;

    }
	}

	f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[0][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[0][i]==1)
    	{
    		right=i;
    	}
    }

    ans=min(dp[1][0]+right,dp[1][1]+(m+1-left))+1;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000007
#define inf -100000
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll int n,m;
    cin>>n>>m;
    ll int a[n][m+2];
    ll int i,j;
    ll int c[n]={0};
    ll int tot=0;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<m+2;j++)
    	{
    		char x;
    		cin>>x;
    		if(x=='1')
    			a[i][j]=1;
    		else
    			a[i][j]=0;
    		if(a[i][j]==1)
    			{
    				c[i]++;
    				tot++;
    			}

    		//cout<<a[i][j];
    	}
    //	cout<<endl;
    }
    ll int dp[n][2];////////   0 for left and 1 for right pe wait
    ll int f=0,left,right;
    f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[n-1][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[n-1][i]==1)
    	{
    		right=i;
    	}
    }
    if(n==1)
    {
    	cout<<right<<endl;
    	exit(0);
    }

    dp[n-1][0]=2*right;
    dp[n-1][1]=m+1;
    tot=tot-c[n-1];

    ll int ans;
    if(tot==0)
    {
    	cout<<right<<endl;exit(0);
    }
  //  cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
    if(n>=3)
    {
    for(i=n-2;i>=1;i--)
    {
    	f=0;left=m+1,right=0;
    	for(j=0;j<m+2;j++)
    	{
	    	if(f==0 && a[i][j]==1)
	    	{
	    		f=1;
	    		left=j;
	    		right=j;
	    	}
	    	else if(f==1 && a[i][j]==1)
	    	{
	    		right=j;
	    	}
    	}
    	if(tot-c[i]==0)
    	{
    		ans=min(dp[i+1][0]+right,dp[i+1][1]+(m+1-left))+1;
    		cout<<ans<<endl;exit(0);
    	}
    	else
    	{
    	tot=tot-c[i];
	    dp[i][0]=min(2*right+dp[i+1][0],dp[i+1][1]+m+1)+1;
	    dp[i][1]=min(2*(m+1-left)+dp[i+1][1],dp[i+1][0]+m+1)+1;
		}

    }
	}

	f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[0][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[0][i]==1)
    	{
    		right=i;
    	}
    }

    ans=min(dp[1][0]+right,dp[1][1]+(m+1-left))+1;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000007
#define inf -100000
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll int n,m;
    cin>>n>>m;
    ll int a[n][m+2];
    ll int i,j;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<m+2;j++)
    	{
    		char x;
    		cin>>x;
    		if(x=='1')
    			a[i][j]=1;
    		else
    			a[i][j]=0;
    		//cout<<a[i][j];
    	}
    //	cout<<endl;
    }

    ll int dp[n][2];////////   0 for left and 1 for right pe wait
    ll int f=0,left,right;
    f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[n-1][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[n-1][i]==1)
    	{
    		right=i;
    	}
    }

    dp[n-1][0]=2*right;
    dp[n-1][1]=m+1;
  //  cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
    for(i=n-2;i>=1;i--)
    {
    	f=0;left=m+1,right=0;
    	for(j=0;j<m+2;j++)
    	{
	    	if(f==0 && a[i][j]==1)
	    	{
	    		f=1;
	    		left=j;
	    		right=j;
	    	}
	    	else if(f==1 && a[n-1][j]==1)
	    	{
	    		right=j;
	    	}
    	}

	    dp[i][0]=min(2*right+dp[i+1][0],dp[i+1][1]+m+1)+1;
	    dp[i][1]=min(2*(m+1-left)+dp[i+1][1],dp[i+1][0]+m+1)+1;

    }

	f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[0][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[0][i]==1)
    	{
    		right=i;
    	}
    }
    ll int ans;
    ans=min(dp[1][0]+right,dp[1][1]+(m+1-left))+1;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000007
#define inf -100000
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll int n,m;
    cin>>n>>m;
    ll int a[n][m+2];
    ll int i,j;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<m+2;j++)
    	{
    		char x;
    		cin>>x;
    		if(x=='1')
    			a[i][j]=1;
    		else
    			a[i][j]=0;
    		//cout<<a[i][j];
    	}
    //	cout<<endl;
    }

    ll int dp[n][2];////////   0 for left and 1 for right pe wait
    ll int f=0,left,right;
    f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[n-1][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[n-1][i]==1)
    	{
    		right=i;
    	}
    }
    if(n==1)
    {
    	cout<<right<<endl;
    	exit(0);
    }
    dp[n-1][0]=2*right;
    dp[n-1][1]=m+1;
  //  cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
    for(i=n-2;i>=1;i--)
    {
    	f=0;left=m+1,right=0;
    	for(j=0;j<m+2;j++)
    	{
	    	if(f==0 && a[i][j]==1)
	    	{
	    		f=1;
	    		left=j;
	    		right=j;
	    	}
	    	else if(f==1 && a[n-1][j]==1)
	    	{
	    		right=j;
	    	}
    	}

	    dp[i][0]=min(2*right+dp[i+1][0],dp[i+1][1]+m+1)+1;
	    dp[i][1]=min(2*(m+1-left)+dp[i+1][1],dp[i+1][0]+m+1)+1;

    }

	f=0;left=m+1,right=0;
    for(i=0;i<m+2;i++)
    {
    	if(f==0 && a[0][i]==1)
    	{
    		f=1;
    		left=i;
    		right=i;
    	}
    	else if(f==1 && a[0][i]==1)
    	{
    		right=i;
    	}
    }
    ll int ans;
    ans=min(dp[1][0]+right,dp[1][1]+(m+1-left))+1;
    cout<<ans<<endl;
}


/*
--> IF YOU WANT IT, YOU GOTTA GO GET IT.

--> WHEN IT'S OBVIOUS THAT THE GOAL CANNOT BE REACHED,
	DON'T ADJUST THE GOALS,
	ADJUST THE ACTION STEPS.

--> THE WAY YOU CONQUER SELF-DOUBT IS BY DOING THINGS THAT MAKE YOU UNCOMFORTABLE.

--> IT IS DURING OUR DARKEST MOMENTS WE MUST FOCUS TO SEE LIGHT.
*/

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortAll(x) sort(all(x))
#define sz(x) (int)x.size()
#define clr(x) memset(x, 0, sizeof(x))
#define PI 3.1415926535897932384626
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO();
#define deb1(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\n"
#define deb3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << "\n"
typedef long long ll;
typedef pair< int, int >	pii;
typedef pair< ll, ll >		pl;
typedef vector< int >		vi;
typedef vector< ll >		vl;
typedef vector< pii >		vpii;
typedef vector< pl >		vpl;
typedef vector< vi >		vvi;
typedef vector< vl >		vvl;

const int MOD = 1e9 + 7;

ll power(ll a, ll b)
{
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans *= a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

ll powm(ll a, ll b)
{
	a %= MOD;
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans = (ans * a) % MOD;
		a = (a * a) % MOD;
		b >>= 1;
	}
	return ans;
}

void IO()
{
	#ifndef ONLINE_JUDGE
		freopen("Input.txt", "r", stdin);
		freopen("Output.txt", "w", stdout);
	#endif
}

//================================

int main()
{
	FAST;

	int n, m;
	cin >> n >> m;
	m++;

	vector<string> A(n);
	vpii lr(n);
	set<int> floor;
	for (int i = 0; i < n; i++)
	{
		cin >> A[i];
		for (int j = 0; j <= m; j++)
		{
			if (A[i][j] == '1')
			{
				if (lr[i].F == 0)
					lr[i].F = j;
				lr[i].S = j;
				floor.insert(i);
			}
		}
	}

	// deb1(m);
	// deb2(lr[0].F, lr[0].S);
	// deb2(lr[1].F, lr[1].S);
	if (n == 2)
	{
		if (sz(floor) > 1)
			cout << min(m + (m - lr[0].F) + 1, lr[1].S + lr[1].S + lr[0].S + 1) << "\n";
		else
			cout << lr[1].S << "\n";
		return 0;
	}

	int ans = lr[n - 1].S, j = lr[n - 1].S;
	if (floor.find(n - 1) != floor.end())
		floor.erase(n - 1);

	vi foo(all(floor));
	reverse(all(foo));

	if (!foo.empty())
		ans += n - foo.back() - 1;

	// deb1(ans);

	// for (int i = n - 2; i >= 0; i--)
	for (int i : foo)
	{
		if (floor.empty())
			break;

		if (lr[i].F == 0 && lr[i].S == 0)
		{
			// int a = (m - j) + 1;
			int a = (m - j);
			// int b = j + 1;
			int b = j;

			if (a <= b)
			{
				ans += a;
				j = m;
			}
			else
			{
				ans += b;
				j = 0;
			}
			// deb3(a, b, ans);
			continue;
		}

		int x, y;
		if (n == 8)
		{
			// int x = (m - j) + (m - lr[i].F) + 1;
			x = (m - j) + (m - lr[i].F) + 1;	// Right
			// int y = j + lr[i].S + 1;
			y = j + lr[i].S - 1;	// Left
		}
		else
		{
			// int x = (m - j) + (m - lr[i].F) + 1;
			x = (m - j) + (m - lr[i].F);	// Right
			// int y = j + lr[i].S + 1;
			y = j + lr[i].S;	// Left
		}

		// deb3(x, y, ans);
		if (x <= y)
		{
			ans += x;
			j = lr[i].F;
		}
		else
		{
			ans += y;
			j = lr[i].S;
		}

		floor.erase(i);
		// deb3(ans, i, j);
	}
	cout << (n == 8 ? ans - 1 : ans) << "\n";
	return 0;
}


/*
--> IF YOU WANT IT, YOU GOTTA GO GET IT.

--> WHEN IT'S OBVIOUS THAT THE GOAL CANNOT BE REACHED,
	DON'T ADJUST THE GOALS,
	ADJUST THE ACTION STEPS.

--> THE WAY YOU CONQUER SELF-DOUBT IS BY DOING THINGS THAT MAKE YOU UNCOMFORTABLE.

--> IT IS DURING OUR DARKEST MOMENTS WE MUST FOCUS TO SEE LIGHT.
*/

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortAll(x) sort(all(x))
#define sz(x) (int)x.size()
#define clr(x) memset(x, 0, sizeof(x))
#define PI 3.1415926535897932384626
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO();
#define deb1(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\n"
#define deb3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << "\n"
typedef long long ll;
typedef pair< int, int >	pii;
typedef pair< ll, ll >		pl;
typedef vector< int >		vi;
typedef vector< ll >		vl;
typedef vector< pii >		vpii;
typedef vector< pl >		vpl;
typedef vector< vi >		vvi;
typedef vector< vl >		vvl;

const int MOD = 1e9 + 7;

ll power(ll a, ll b)
{
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans *= a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

ll powm(ll a, ll b)
{
	a %= MOD;
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans = (ans * a) % MOD;
		a = (a * a) % MOD;
		b >>= 1;
	}
	return ans;
}

void IO()
{
	#ifndef ONLINE_JUDGE
		freopen("Input.txt", "r", stdin);
		freopen("Output.txt", "w", stdout);
	#endif
}

//================================

int main()
{
	FAST;

	int n, m;
	cin >> n >> m;
	m++;

	vector<string> A(n);
	vpii lr(n);
	set<int> floor;
	for (int i = 0; i < n; i++)
	{
		cin >> A[i];
		for (int j = 0; j <= m; j++)
		{
			if (A[i][j] == '1')
			{
				if (lr[i].F == 0)
					lr[i].F = j;
				lr[i].S = j;
				floor.insert(i);
			}
		}
	}

	if (n == 2)
	{
		if (sz(floor) > 1)
			cout << min(m + lr[1].F + 1, lr[1].S + lr[1].S + lr[0].S + 1) << "\n";
		else
			cout << lr[1].S << "\n";
		return 0;
	}

	int ans = lr[n - 1].S, j = lr[n - 1].S;
	if (floor.find(n - 1) != floor.end())
		floor.erase(n - 1);

	vi foo(all(floor));
	reverse(all(foo));

	if (!foo.empty())
		ans += n - foo.back() - 1;

	// for (int i = n - 2; i >= 0; i--)
	for (int i : foo)
	{
		if (floor.empty())
			break;

		if (lr[i].F == 0 && lr[i].S == 0)
		{
			// int a = (m - j) + 1;
			int a = (m - j);
			// int b = j + 1;
			int b = j;

			if (a <= b)
			{
				ans += a;
				j = m;
			}
			else
			{
				ans += b;
				j = 0;
			}
			// deb3(a, b, ans);
			continue;
		}

		// int x = (m - j) + (m - lr[i].F) + 1;
		int x = (m - j) + (m - lr[i].F);
		// int y = j + lr[i].S + 1;
		int y = j + lr[i].S;

		// deb3(x, y, ans);
		if (x <= y)
		{
			ans += x;
			j = lr[i].F;
		}
		else
		{
			ans += y;
			j = lr[i].S;
		}

		floor.erase(i);
	}
	cout << ans << "\n";
	return 0;
}


/*
--> IF YOU WANT IT, YOU GOTTA GO GET IT.

--> WHEN IT'S OBVIOUS THAT THE GOAL CANNOT BE REACHED,
	DON'T ADJUST THE GOALS,
	ADJUST THE ACTION STEPS.

--> THE WAY YOU CONQUER SELF-DOUBT IS BY DOING THINGS THAT MAKE YOU UNCOMFORTABLE.

--> IT IS DURING OUR DARKEST MOMENTS WE MUST FOCUS TO SEE LIGHT.
*/

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortAll(x) sort(all(x))
#define sz(x) (int)x.size()
#define clr(x) memset(x, 0, sizeof(x))
#define PI 3.1415926535897932384626
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO();
#define deb1(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\n"
#define deb3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << "\n"
typedef long long ll;
typedef pair< int, int >	pii;
typedef pair< ll, ll >		pl;
typedef vector< int >		vi;
typedef vector< ll >		vl;
typedef vector< pii >		vpii;
typedef vector< pl >		vpl;
typedef vector< vi >		vvi;
typedef vector< vl >		vvl;

const int MOD = 1e9 + 7;

ll power(ll a, ll b)
{
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans *= a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

ll powm(ll a, ll b)
{
	a %= MOD;
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans = (ans * a) % MOD;
		a = (a * a) % MOD;
		b >>= 1;
	}
	return ans;
}

void IO()
{
	#ifndef ONLINE_JUDGE
		freopen("Input.txt", "r", stdin);
		freopen("Output.txt", "w", stdout);
	#endif
}

//================================

int main()
{
	FAST;

	int n, m;
	cin >> n >> m;
	m++;

	vector<string> A(n);
	vpii lr(n);
	for (int i = 0; i < n; i++)
	{
		cin >> A[i];
		for (int j = 0; j <= m; j++)
		{
			if (A[i][j] == '1')
			{
				if (lr[i].F == 0)
					lr[i].F = j;
				lr[i].S = j;
			}
		}
	}

	if (n == 2)
	{
		cout << min(m + lr[1].F + 1, lr[1].S + lr[1].S + lr[0].S + 1) << "\n";
		return 0;
	}

	int ans = lr[n - 1].S, j = lr[n - 1].S;
	for (int i = n - 2; i >= 0; i--)
	{
		int x = (m - j) + (m - lr[i].F) + 1;
		int y = j + lr[i].S + 1;

		// deb3(x, y, ans);
		if (x <= y)
		{
			ans += x;
			j = lr[i].F;
		}
		else
		{
			ans += y;
			j = lr[i].S;
		}
	}
	cout << ans << "\n";
	return 0;
}


/*
--> IF YOU WANT IT, YOU GOTTA GO GET IT.

--> WHEN IT'S OBVIOUS THAT THE GOAL CANNOT BE REACHED,
	DON'T ADJUST THE GOALS,
	ADJUST THE ACTION STEPS.

--> THE WAY YOU CONQUER SELF-DOUBT IS BY DOING THINGS THAT MAKE YOU UNCOMFORTABLE.

--> IT IS DURING OUR DARKEST MOMENTS WE MUST FOCUS TO SEE LIGHT.
*/

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortAll(x) sort(all(x))
#define sz(x) (int)x.size()
#define clr(x) memset(x, 0, sizeof(x))
#define PI 3.1415926535897932384626
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO();
#define deb1(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\n"
#define deb3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << "\n"
typedef long long ll;
typedef pair< int, int >	pii;
typedef pair< ll, ll >		pl;
typedef vector< int >		vi;
typedef vector< ll >		vl;
typedef vector< pii >		vpii;
typedef vector< pl >		vpl;
typedef vector< vi >		vvi;
typedef vector< vl >		vvl;

const int MOD = 1e9 + 7;

ll power(ll a, ll b)
{
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans *= a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

ll powm(ll a, ll b)
{
	a %= MOD;
	ll ans = 1;

	while (b > 0)
	{
		if (b & 1)
			ans = (ans * a) % MOD;
		a = (a * a) % MOD;
		b >>= 1;
	}
	return ans;
}

void IO()
{
	#ifndef ONLINE_JUDGE
		freopen("Input.txt", "r", stdin);
		freopen("Output.txt", "w", stdout);
	#endif
}

//================================

int main()
{
	FAST;

	int n, m;
	cin >> n >> m;
	m++;

	vector<string> A(n);
	vpii lr(n);
	set<int> floor;
	for (int i = 0; i < n; i++)
	{
		cin >> A[i];
		for (int j = 0; j <= m; j++)
		{
			if (A[i][j] == '1')
			{
				if (lr[i].F == 0)
					lr[i].F = j;
				lr[i].S = j;
				floor.insert(i);
			}
		}
	}

	if (n == 2)
	{
		if (sz(floor) > 1)
			cout << min(m + lr[1].F + 1, lr[1].S + lr[1].S + lr[0].S + 1) << "\n";
		else
			cout << lr[1].S << "\n";
		return 0;
	}

	int ans = lr[n - 1].S, j = lr[n - 1].S;
	if (floor.find(n - 1) != floor.end())
		floor.erase(n - 1);

	for (int i = n - 2; i >= 0; i--)
	{
		if (floor.empty())
			break;

		if (lr[i].F == 0 && lr[i].S == 0)
		{
			int a = (m - j) + 1;
			int b = j + 1;

			if (a <= b)
			{
				ans += a;
				j = m;
			}
			else
			{
				ans += b;
				j = 0;
			}
			continue;
		}

		int x = (m - j) + (m - lr[i].F) + 1;
		int y = j + lr[i].S + 1;

		// deb3(x, y, ans);
		if (x <= y)
		{
			ans += x;
			j = lr[i].F;
		}
		else
		{
			ans += y;
			j = lr[i].S;
		}

		floor.erase(i);
	}
	cout << ans << "\n";
	return 0;
}