#include <bits/stdc++.h>
using namespace std;
int n, m, l_r_index[18][2], dp[18][2], r, last = -1, start = 1e5, answer;
string a;
int main() {
  scanf("%d%d", &n, &m);
  m += 2;
  for (int i = 0; i < n; i++) {
    r = -1;
    cin >> a;
    for (int j = 0; j < m; j++) {
      if (a[j] == '1') {
        start = i;
        if (last == -1) last = i;
        if (r == -1) {
          l_r_index[i][0] = j;
          r = 1;
        }
        l_r_index[i][1] = j;
      }
    }
  }
  if (last == -1) return puts("0");
  m -= 2;
  dp[start][0] = (l_r_index[start][1]) * 2;
  dp[start][1] = m + 1;
  for (int i = start - 1; i > last; i--) {
    dp[i][0] = min(dp[i + 1][0] + (2 * l_r_index[i][1]), dp[i + 1][1] + m + 1);
    if (l_r_index[i][0] == 0)
      dp[i][1] = min(dp[i + 1][0] + m + 1, dp[i + 1][1]);
    else
      dp[i][1] = min(dp[i + 1][0] + m + 1,
                     dp[i + 1][1] + (2 * (m + 1 - l_r_index[i][0])));
  }
  if (start != last) {
    answer = min(dp[last + 1][0] + l_r_index[last][1],
                 dp[last + 1][1] + 1 + m - l_r_index[last][0]) +
             (n - last - 1);
  } else {
    answer = l_r_index[start][1] + (n - last - 1);
  }
  cout << answer << " \n";
  return 0;
}
