#include <bits/stdc++.h>
using namespace std;
int n, m, l[1000], r[1000], last = 1e9, dp[16][3];
int solve(int ind, int dirn) {
  if (ind == last) {
    if (dirn == 0) {
      return r[ind];
    } else {
      return (m + 1 - l[ind]);
    }
  }
  if (dp[ind][dirn] != -1) {
    return dp[ind][dirn];
  }
  int ans = 1e9;
  if (l[ind] == 0) {
    if (dirn == 0) {
      int cost = 1;
      ans = min(ans, solve(ind - 1, 0) + cost);
      cost = m + 2;
      ans = min(ans, solve(ind - 1, 1) + cost);
    } else {
      int cost = m + 2;
      ans = min(ans, solve(ind - 1, 0) + cost);
      cost = 1;
      ans = min(ans, solve(ind - 1, 1) + cost);
    }
  } else {
    if (dirn == 0) {
      int cost = 2 * r[ind] + 1;
      ans = min(ans, solve(ind - 1, 0) + cost);
      cost = m + 2;
      ans = min(ans, solve(ind - 1, 1) + cost);
    } else {
      int cost = m + 2;
      ans = min(ans, solve(ind - 1, 0) + cost);
      cost = (2 * (m + 1 - l[ind])) + 1;
      ans = min(ans, solve(ind - 1, 1) + cost);
    }
  }
  return (dp[ind][dirn] = ans);
}
int main() {
  string s[16];
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m + 2; j++) {
      if (s[i][j] == '1' && l[i] == 0) {
        l[i] = j;
      }
      if (s[i][j] == '1') {
        r[i] = j;
        last = min(last, i);
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  if (last > n) {
    cout << "0";
  } else {
    cout << solve(n - 1, 0);
  }
  return 0;
}
