#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
string str[20];
int stop, start1[20], end1[20], dp[20][2];
int n, m;
int go(int i, bool flag) {
  if (dp[i][flag] != -1) return dp[i][flag];
  int ret = 0;
  if (flag) {
    if (i == stop) {
      ret = end1[i];
    } else {
      ret = 2 * (end1[i]) + go(i - 1, 1) + 1;
      ret = min(ret, go(i - 1, 0) + m + 2);
    }
  } else {
    if (i == stop) {
      ret = m - start1[i] + 1;
    } else {
      ret = 2 * (m - start1[i] + 1) + 1 + go(i - 1, 0);
      ret = min(ret, go(i - 1, 1) + m + 2);
    }
  }
  return dp[i][flag] = ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> str[i];
  stop = -1;
  for (i = 1; i <= n; i++) {
    start1[i] = m + 1;
    for (j = 1; j <= m; j++) {
      if (str[i][j] == '1') {
        if (stop == -1) stop = i;
        if (start1[i] == m + 1) start1[i] = j;
        end1[i] = j;
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  if (stop != -1) ans = go(n, 1);
  cout << ans;
  return 0;
}
