#include <bits/stdc++.h>
using namespace std;
long long n, m, dp[2][2][2][52];
vector<string> v;
long long mini(long long a, long long b) { return a > b ? b : a; }
long long func(long long d, long long a, long long c, long long pos) {
  if (dp[d][a][c][pos] != -1) return dp[d][a][c][pos];
  if (pos == n) {
    if (d != 1 || a != 1 || c != 1)
      return 10000001;
    else
      return 0;
  }
  long long ret = 10000001, i, val, dist;
  for (i = 0; i < m; i++) {
    dist = mini(i, m - i);
    if (v[pos][i] >= 48 && v[pos][i] <= 57) {
      val = func(1, a, c, pos + 1);
    } else if (v[pos][i] >= 97 && v[pos][i] <= 122) {
      val = func(d, 1, c, pos + 1);
    } else {
      val = func(d, a, 1, pos + 1);
    }
    if (dist + val < ret) ret = dist + val;
  }
  return dp[d][a][c][pos] = ret;
}
int main() {
  long long i;
  memset(dp, -1, sizeof(dp));
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  v.resize(n);
  for (i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << func(0, 0, 0, 0) << "\n";
  return 0;
}
