#include <bits/stdc++.h>
using namespace std;
char ying[20][105];
int lou[20];
int leftt[20], rightt[20];
int shu[40000];
int main() {
  int n, m, i, j, k, ans, num, a, b, c, d, a2, b2, c2, d2, noww1, noww2, sym,
      n2, xx;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  while (cin >> n >> m) {
    memset(lou, 0, sizeof(lou));
    memset(leftt, 0, sizeof(leftt));
    memset(rightt, 0, sizeof(rightt));
    for (i = n; i >= 1; i--) {
      cin >> ying[i] + 1;
    }
    for (i = n; i >= 1; i--)
      for (j = 1; j <= m + 2; j++) {
        if (ying[i][j] == '1') lou[i]++;
      }
    int lala = 0;
    for (i = 1; i <= n; i++) {
      if (lou[i] == 0) lala++;
    }
    xx = 0;
    for (i = n; i >= 1; i--) {
      if (lou[i] == 0)
        xx++;
      else
        break;
    }
    for (i = 1; i <= n - lala; i++) {
      while (lou[i] == 0) {
        for (j = i; j < n; j++) {
          for (k = 1; k <= m + 2; k++) {
            ying[j][k] = ying[j + 1][k];
          }
          lou[j] = lou[j + 1];
        }
      }
    }
    n = n - lala;
    if (n == 0) {
      cout << "0" << endl;
      continue;
    }
    for (i = n; i >= 1; i--) {
      sym = 0;
      for (j = 1; j <= m + 2; j++) {
        if (ying[i][j] == '1') {
          lou[i]++;
          if (sym == 0) {
            leftt[i] = j;
            sym = 1;
          }
        }
        if (ying[i][j] == '1') rightt[i] = j;
        if (j == m + 2 && leftt[i] == 0) leftt[i] = 1;
        if (j == m + 2 && rightt[i] == 0 && leftt[i] != 1) rightt[i] = leftt[i];
        if (j == m + 2 && rightt[i] == 0 && leftt[i] == 1) rightt[i] = m + 2;
      }
    }
    j = 0, ans = 999999999;
    noww1 = noww2 = rightt[1];
    num = 2;
    shu[1] = rightt[1] - 1;
    i = a2 = 1;
    int bian = 1, qi;
    while (num <= n) {
      qi = 0;
      for (i = a2; i <= bian; i++) {
        if (qi == 0) {
          a2 = i * 2;
          qi = 1;
        }
        shu[i * 2] = shu[i] + noww1 - 1 + rightt[num];
        shu[i * 2 + 1] = shu[i] + m + m + 5 - leftt[num] - noww1;
        if (i == bian)
          noww1 = rightt[num];
        else if (noww1 == rightt[num - 1])
          noww1 = leftt[num - 1];
        else
          noww1 = rightt[num - 1];
      }
      num++;
      bian = (i - 1) * 2 + 1;
    }
    for (j = pow(2, n - 1); j <= bian; j++) {
      ans = min(ans, shu[j]);
    }
    cout << ans + lala - xx << endl;
  }
  return 0;
}
