#include <bits/stdc++.h>
using namespace std;
int n, m;
int tp = -1;
int lef[105];
int rig[105];
int dp[20][2];
void read() {
  memset(lef, 0, sizeof(lef));
  memset(rig, 0, sizeof(rig));
  char c;
  cin >> n >> m;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j < m + 2; j++) {
      cin >> c;
      if (c == '1') {
        lef[i] = j;
        if (rig[i] == 0) rig[i] = m + 1 - j;
        if (tp == -1) tp = i;
      }
    }
  }
}
void solve() {
  memset(dp, 0, sizeof(dp));
  dp[1][0] = lef[1] * 2;
  dp[1][1] = m + 1;
  for (int i = 1; i <= n; i++) {
    dp[i + 1][0] = min(dp[i][0] + lef[i + 1] * 2 + 1, dp[i][1] + m + 2);
    dp[i + 1][1] = min(dp[i][1] + rig[i + 1] * 2 + 1, dp[i][0] + m + 2);
  }
}
void print() {
  if (tp != -1) {
    if (tp != 1)
      cout << min(dp[tp - 1][0] + lef[tp] + 1, dp[tp - 1][1] + rig[tp] + 1)
           << endl;
    else
      cout << lef[tp] << endl;
  } else
    cout << 0 << endl;
}
int main() {
  read();
  solve();
  print();
}
