alexjercan/bugnet huggingface dataset:
Problem p00875 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int n;
vector<string> a(10);
vector<string> b(10);
string g, d;

queue<string> que;

string repl(string s, int i) {
  string s2 = s.substr(a[i].size());
  int x = s2.find(a[i]);
  if (x != string::npos) {
    string s3 = s2.substr(0, x) + repl(s2.substr(x), i);
    return b[i] + s3;
  } else
    return b[i] + s2;
}

int bfs(int c) {
  if (c > 9)
    return -1;
  queue<string> que2;
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < n; i++) {
      int x = s.find(a[i]);
      if (x == string::npos)
        continue;
      string s2 = s.substr(x);
      string s3 = s.substr(0, x) + repl(s2, i);
      if (s3 == d)
        return c + 1;
      que2.push(s3);
    }
  }
  que = que2;
  return bfs(c + 1);
}

int main(void) {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> g >> d;
    queue<string> a;
    que = a;
    que.push(g);
    cout << bfs(0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int n;
vector<string> a(10);
vector<string> b(10);
string g, d;
queue<string> que;

string repl(string s, int i) {
  string s2 = s.substr(a[i].size());
  if (s2.find(a[i]) != string::npos)
    return b[i] + repl(s2, i);
  else
    return b[i] + s2;
}

int bfs(int c) {
  if (c > 10)
    return -1;
  queue<string> que2;
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < n; i++) {
      int x = s.find(a[i]);
      if (x == string::npos)
        continue;
      string s2 = s.substr(x);
      string s3 = s.substr(0, x) + repl(s2, i);
      if (s3 == d)
        return c + 1;
      que2.push(s3);
    }
  }
  que = que2;
  return bfs(c + 1);
}

int main(void) {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> g >> d;
    queue<string> a;
    que = a;
    que.push(g);
    cout << bfs(0) << endl;
  }
}
Problem p00875 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int, string> pis;
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> alpha(n), beta(n);
    for (int i = 0; i < n; i++) {
      cin >> alpha[i] >> beta[i];
    }
    string s, t;
    cin >> s >> t;
    queue<pis> que;
    que.push(mp(0, s));
    bool flag = false;
    while (que.size()) {
      int cnt = que.front().first;
      s = que.front().second;
      que.pop();
      if (s == t) {
        cout << cnt << endl;
        flag = true;
        break;
      }
      if (cnt > 10) {
        cout << -1 << endl;
        flag = true;
        break;
      }

      for (int i = 0; i < alpha.size(); i++) {
        vector<int> pos;
        string tmp = s;
        for (int j = 0; j <= (int)s.length() - (int)alpha[i].length(); j++) {
          if (s.substr(j, alpha[i].length()) == alpha[i])
            pos.push_back(j), j += alpha[i].length() - 1;
        }
        for (int j = pos.size() - 1; j >= 0; j--) {
          tmp = tmp.substr(0, pos[j]) + beta[i] +
                tmp.substr(pos[j] + alpha[i].length());
        }
        if (s != tmp && tmp.length() <= t.length()) {
          que.push(mp(cnt + 1, tmp));
        }
      }
    }
    if (flag == false) {
      cout << -1 << endl;
    }
  }
}

Error: WA

Fixed submission:


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int, string> pis;
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> alpha(n), beta(n);
    for (int i = 0; i < n; i++) {
      cin >> alpha[i] >> beta[i];
    }
    string s, t;
    cin >> s >> t;
    queue<pis> que;
    // cout << "s:" << s << endl;
    que.push(mp(0, s));
    bool flag = false;
    while (que.size()) {
      int cnt = que.front().first;
      s = que.front().second;
      que.pop();
      if (s == t) {
        cout << cnt << endl;
        flag = true;
        break;
      }
      if (cnt > 10) {
        cout << -1 << endl;
        flag = true;
        break;
      }
      if (s.length() > t.length())
        continue;

      // cout << s << endl;
      for (int i = 0; i < alpha.size(); i++) {
        vector<int> pos;
        string tmp = s;
        for (int j = 0; j <= s.length() - alpha[i].length(); j++) {
          if (s.substr(j, alpha[i].length()) == alpha[i])
            pos.push_back(j);
        }
        for (int j = pos.size() - 1; j >= 0; j--) {
          cout << "tmp:" << tmp.substr(0, 0) << endl;
          tmp = tmp.substr(0, pos[j]) + beta[i] +
                tmp.substr(pos[j] + alpha[i].length());
        }
        if (s != tmp) {
          que.push(mp(cnt + 1, tmp));
        }
      }
    }
    if (flag == false) {
      cout << -1 << endl;
    }
  }
}
Problem p00875 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <vector>
#define inf (1 << 29)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAX 100
using namespace std;
int N;
string alpha[MAX];
string beta[MAX];
string s, t;

string replaceAll(string s, string f, string t) {
  string r;
  for (int p = 0; (p = s.find(f)) != s.npos;) {
    r += s.substr(0, p) + t;
    s = s.substr(p + f.size());
  }
  return r + s;
}

struct P {
  string s;
  int cost;
  P(string s = "", int cost = -inf) : s(s), cost(cost) {}
};

int main() {
  /*
  string s = "nariXXXnarinanariYO"; // これを
  string f = "nari";                // 置換したいstring
  string t = "ir";                  // 置換するstring
  cout << replaceAll(s,f,t) << endl;
  */
  while (cin >> N, N) {
    rep(i, N) cin >> alpha[i] >> beta[i];
    cin >> s >> t;
    deque<P> deq;
    int mincost = inf;
    int tsize = t.size();
    set<string> memo;
    deq.push_back(P(s, 0));
    while (!deq.empty()) {
      P p = deq.front();
      deq.pop_front();
      if (tsize < p.s.size())
        continue;
      if (p.s == t) {
        mincost = p.cost;
        break;
      }

      if (memo.find(p.s) == memo.end())
        memo.insert(p.s);
      else
        continue;

      rep(i, N) {
        string pres = p.s;
        pres = replaceAll(pres, alpha[i], beta[i]);
        deq.push_back(P(pres, p.cost + 1));
      }
    }
    cout << (mincost == inf ? -1 : mincost) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#define inf (1 << 29)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAX 100
using namespace std;
int N;
string alpha[MAX];
string beta[MAX];
string s, t;

string replaceAll(string s, string f, string t) {
  string r;
  for (int p = 0; (p = s.find(f)) != s.npos;) {
    r += s.substr(0, p) + t;
    s = s.substr(p + f.size());
  }
  return r + s;
}

struct P {
  string s;
  int cost;
  P(string s = "", int cost = -inf) : s(s), cost(cost) {}
};

int main() {
  while (cin >> N, N) {
    rep(i, N) cin >> alpha[i] >> beta[i];
    cin >> s >> t;
    deque<P> deq;
    int mincost = inf;
    int tsize = t.size();
    deq.push_back(P(s, 0));
    while (!deq.empty()) {
      P p = deq.front();
      deq.pop_front();
      if (tsize < p.s.size())
        continue;
      if (p.s == t) {
        mincost = p.cost;
        break;
      }
      rep(i, N) {
        string pres = p.s;
        pres = replaceAll(pres, alpha[i], beta[i]);
        deq.push_back(P(pres, p.cost + 1));
      }
    }
    cout << (mincost == inf ? -1 : mincost) << endl;
  }
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
  // cout << v << endl;
  if (v == t) {
    res = min(res, c);
    return;
  }
  if (v.size() >= t.size() || c >= res)
    return;
  for (int i = 0; i < n; i++) {
    memset(used, 0, sizeof(used));
    bool flag = false;
    if (v.size() < a[i].size())
      continue;
    for (int j = 0; j <= v.size() - a[i].size(); j++) {
      if (v.substr(j, a[i].size()) == a[i]) {
        used[j] = true;
        j += a[i].size() - 1;
        flag = true;
      }
    }
    if (!flag)
      continue;
    string next = "";
    for (int j = 0; j < v.size(); j++) {
      if (!used[j])
        next += v[j];
      else {
        next += b[i];
        j += a[i].size() - 1;
      }
    }
    dfs(next, c + 1);
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> f;
    cin >> t;
    res = t.size() - f.size() + 1;
    dfs(f, 0);
    printf("%d\\n", res == t.size() - f.size() + 1 ? -1 : res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
  // cout << v << endl;
  if (v == t) {
    res = min(res, c);
    return;
  }
  if (v.size() >= t.size() || c >= res)
    return;
  for (int i = 0; i < n; i++) {
    memset(used, 0, sizeof(used));
    bool flag = false;
    for (int j = 0; j <= v.size() - a[i].size(); j++) {
      if (v.substr(j, a[i].size()) == a[i]) {
        used[j] = true;
        j += a[i].size() - 1;
        flag = true;
      }
    }
    if (!flag)
      continue;
    string next = "";
    for (int j = 0; j < v.size(); j++) {
      if (!used[j])
        next += v[j];
      else {
        next += b[i];
        j += a[i].size() - 1;
      }
    }
    dfs(next, c + 1);
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> f;
    cin >> t;
    res = t.size() - f.size() + 1;
    dfs(f, 0);
    printf("%d\\n", res == t.size() - f.size() + 1 ? -1 : res);
  }
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string sed(string s, string a, string b) {
  int n = s.size();
  int m = a.size();
  string ret;

  for (int i = 0; i < n; ++i) {
    if (i + m <= n && s.substr(i, m) == a) {
      ret += b;
      i += m - 1;
    } else {
      ret += s[i];
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    string s, t;
    cin >> s >> t;

    int ret = 0;
    set<string> curr;
    curr.insert(s);
    bool ng = true;
    while (!curr.empty()) {
      set<string> next;
      set<string>::iterator it;
      for (it = curr.begin(); it != curr.end(); ++it) {
        if (*it == t) {
          next.clear();
          ng = false;
          cout << ret << endl;
          break;
        }
        for (int i = 0; i < n; ++i) {
          string x = sed(*it, a[i], b[i]);
          if (x != *it && x.size() <= t.size())
            next.insert(x);
        }
      }
      curr.swap(next);
      ++ret;
    }

    if (ng)
      cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string sed(string s, string a, string b) {
  int n = s.size();
  int m = a.size();
  string ret;

  for (int i = 0; i < n; ++i) {
    if (i + m <= n && s.substr(i, m) == a) {
      ret += b;
      i += m - 1;
    } else {
      ret += s[i];
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    string s, t;
    cin >> s >> t;

    int ret = 0;
    set<string> curr;
    curr.insert(s);
    bool ng = true;
    while (!curr.empty()) {
      set<string> next;
      set<string>::iterator it;
      for (it = curr.begin(); it != curr.end(); ++it) {
        if (*it == t) {
          next.clear();
          ng = false;
          cout << ret << endl;
          break;
        }
        for (int i = 0; i < n; ++i) {
          string x = sed(*it, a[i], b[i]);
          if (x.size() <= t.size())
            next.insert(x);
        }
      }
      curr.swap(next);
      ++ret;
    }

    if (ng)
      cout << -1 << endl;
  }
}
Problem p00876 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define SIZE 200000
using namespace std;
typedef pair<int, int> P;

int n;
P x[50];
int dp[2][SIZE];

void solve() {
  rep(i, 2) rep(j, SIZE) dp[i][j] = 0;
  rep(i, n) cin >> x[i].first >> x[i].second;
  rep(i, n) x[i].second *= 2;
  sort(x, x + n, greater<P>());
  int ans = 0;
  rep(i, n) {
    int dir = 0;
    int ti = 0;
    while (x[i].second) {
      int tmpti = ti;
      if (ti == 0)
        ti = ti + x[i].first;
      else
        ti = max(ti + x[i].first, dp[dir][ti - 1]);
      dp[dir][tmpti] = max(dp[dir][tmpti], ti);
      dir = dir ^ 1;
      x[i].second--;
    }
    rep(j, 2) for (int k = 1; k < SIZE; k++) dp[j][k] =
        max(dp[j][k], dp[j][k - 1]);
  }
  rep(i, 2) rep(j, SIZE) ans = max(ans, dp[i][j]);
  cout << ans << endl;
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
typedef pair<int, int> P;

int n;
P x[50];
int dp[2][100000];

void solve() {
  rep(i, 2) rep(j, 100000) dp[i][j] = 0;
  rep(i, n) cin >> x[i].first >> x[i].second;
  rep(i, n) x[i].second *= 2;
  sort(x, x + n, greater<P>());
  int ans = 0;
  rep(i, n) {
    int dir = 0;
    int ti = 0;
    while (x[i].second) {
      int tmpti = ti;
      if (ti == 0)
        ti = ti + x[i].first;
      else
        ti = max(ti + x[i].first, dp[dir][ti - 1]);
      dp[dir][tmpti] = max(dp[dir][tmpti], ti);
      dir = dir ^ 1;
      x[i].second--;
    }
    rep(j, 2) for (int k = 1; k < 100000; k++) dp[j][k] =
        max(dp[j][k], dp[j][k - 1]);
  }
  rep(i, 2) rep(j, 100000) ans = max(ans, dp[i][j]);
  cout << ans << endl;
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
}
Problem p00881 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
using namespace std;

int M, N;
map<pair<set<int>, set<int>>, int> m;
char F[129][12];

int dfs(set<int> ob, set<int> fe) {
  if (ob.size() <= 1)
    return (M - (int)fe.size());
  if (m.count({ob, fe}))
    return m[{ob, fe}];
  int res = 1e9;
  set<int> fe2 = fe;
  for (auto x : fe) {
    set<int> ob0, ob1;
    for (auto y : ob) {
      if (F[y][x] == '0')
        ob0.insert(y);
      else
        ob1.insert(y);
    }
    fe2.erase(x);
    res = min(res, max(dfs(ob0, fe2), dfs(ob1, fe2)));
    fe2.insert(x);
  }
  return m[{ob, fe}] = res;
}

int main() {
  while (cin >> M >> N && N > 0) {
    m.clear();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        cin >> F[i][j];
    set<int> ob, fe;
    for (int i = 0; i < N; i++)
      ob.insert(i);
    for (int j = 0; j < M; j++)
      fe.insert(j);
    cout << dfs(ob, fe) << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;

int M, N;
char F[130][11];

int main() {
  while (cin >> M >> N && M > 0) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        cin >> F[i][j];
    int ans = 0;
    for (int i = 0; i < N; i++) {
      int res = 1e9;
      for (int S = 0; S < (1 << M); S++) {
        set<string> s;
        string now = "";
        for (int k = 0; k < M; k++)
          if (S & (1 << k))
            now += F[i][k];
        for (int j = 0; j < N; j++) {
          if (i == j)
            continue;
          string T = "";
          for (int k = 0; k < M; k++)
            if (S & (1 << k))
              T += F[j][k];
          s.insert(T);
        }
        bool ok = true;
        for (auto x : s)
          if (x == now)
            ok = false;
        if (ok)
          res = min(res, __builtin_popcount(S));
      }
      ans = max(ans, res);
    }
    cout << ans << endl;
  }
}
Problem p00881 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;

bool issame(string &a, string &b) {
  for (int i = 0; i < (int)a.length(); i++) {
    if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

int solve(string &str, vector<string> &s, map<string, int> &mincost) {
  if (mincost.count(str) != 0) {
    return mincost[str];
  }
  int n = s.size();
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (issame(s[i], str))
      count++;
  }
  if (count <= 1) {
    mincost[str] = 0;
    return 0;
  }

  int m = str.length();
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (str[i] == 'x') {
      string tmpstr[2] = {str, str};
      tmpstr[0][i] = '0';
      tmpstr[1][i] = '1';
      res = min(
          res,
          max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
    }
  }
  mincost[str] = res;
  return res;
}

int main() {
  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      break;

    vector<string> s(n);
    for (int i = 0; i < n; i++)
      cin >> s[i];
    map<string, int> mincost;
    string ini = string(m, 'x');
    cout << solve(ini, s, mincost) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;

bool issame(string a, string b) {
  for (int i = 0; i < (int)a.length(); i++) {
    if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

int solve(string &str, vector<string> &s, map<string, int> &mincost) {
  if (mincost.count(str) != 0) {
    return mincost[str];
  }
  int n = s.size();
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (issame(s[i], str))
      count++;
  }
  if (count <= 1) {
    mincost[str] = 0;
    return 0;
  }

  int m = str.length();
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (str[i] == 'x') {
      string tmpstr[2] = {str, str};
      tmpstr[0][i] = '0';
      tmpstr[1][i] = '1';
      res = min(
          res,
          max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
    }
  }
  mincost[str] = res;
  return res;
}

int main() {
  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      break;

    vector<string> s(n);
    for (int i = 0; i < n; i++)
      cin >> s[i];
    map<string, int> mincost;
    string ini = string(m, 'x');
    cout << solve(ini, s, mincost) << endl;
  }
  return 0;
}
Problem p00881 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int m, n, in[128];
int dp[1600000], pw[13];

int rec(int bit) {
  int &res = dp[bit];
  if (res >= 0)
    return res;

  int t[13] = {}, cnt = 0;
  rep(j, m) t[j] = bit / pw[j] % 3;
  rep(i, n) {
    bool ok = 1;
    rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
      ok = 0;
      break;
    }
    if (ok)
      cnt++;
  }
  if (cnt <= 1)
    return res = 0;
  res = inf;
  rep(i, m) if (t[i] == 2) {
    res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
  }
  return res;
}
int main() {
  pw[0] = 1;
  rep(i, 12) pw[i + 1] = pw[i] * 3;
  while (cin >> m >> n, m) {
    rep(i, n) {
      char buf[20];
      cin >> buf;
      in[i] = 0;
      rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(pw[m] - 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int m, n, in[100];
int dp[1600000], pw[13];

int rec(int bit) {
  int &res = dp[bit];
  if (res >= 0)
    return res;

  int t[13] = {}, cnt = 0;
  rep(j, m) t[j] = bit / pw[j] % 3;
  rep(i, n) {
    bool ok = 1;
    rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
      ok = 0;
      break;
    }
    if (ok)
      cnt++;
  }
  if (cnt <= 1)
    return res = 0;
  res = inf;
  rep(i, m) if (t[i] == 2) {
    res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
  }
  return res;
}
int main() {
  pw[0] = 1;
  rep(i, 12) pw[i + 1] = pw[i] * 3;
  while (cin >> m >> n, m) {
    rep(i, n) {
      char buf[20];
      cin >> buf;
      in[i] = 0;
      rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(pw[m] - 1) << endl;
  }
  return 0;
}