#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[20], u[20];
int tem;
int dp[20][2];
int main() {
  cin >> n >> m;
  for (int i = n - 1; i >= 0; i--) {
    string L;
    cin >> L;
    for (int j = 0; j < L.size(); j++) {
      if (L[j] == '1') {
        if (!p[i]) p[i] = j + 1;
        u[i] = j + 1;
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (u[i]) tem = i + 1;
  if (!tem) {
    printf("0\n");
    return 0;
  }
  dp[0][0] = 0;
  dp[0][1] = m + 1;
  for (int i = 1; i < tem; i++) {
    if (!u[i - 1]) {
      dp[i][0] = dp[i - 1][0] + 1;
      dp[i][1] = dp[i - 1][1] + 1;
    } else {
      dp[i][0] = 1000000000;
      dp[i][0] = min(dp[i][0], dp[i - 1][0] + (u[i - 1] - 1) * 2 + 1);
      dp[i][0] = min(dp[i][0], dp[i - 1][1] + m + 1 + 1);
      dp[i][1] = 1000000000;
      dp[i][1] = min(dp[i][1], dp[i - 1][1] + (m + 1 - (p[i - 1] - 1)) * 2 + 1);
      dp[i][1] = min(dp[i][1], dp[i - 1][0] + m + 1 + 1);
    }
  }
  tem--;
  long long ans =
      min(dp[tem][0] + (u[tem] - 1), dp[tem][1] + (m + 1 - (p[tem] - 1)));
  cout << ans << endl;
}
