#include <bits/stdc++.h>
using namespace std;
string pass[55];
int mem[55][55][2][2][2];
int n, m;
bool low(char x) { return ('a' <= x && x <= 'z'); }
bool dig(char x) { return ('0' <= x && x <= '9'); }
bool sym(char x) { return (x == '*' || x == '#' || x == '&'); }
int dis(int x, int y) {
  int ans = abs(x - y);
  ans = min(ans, m - max(x, y) + min(x, y));
  return ans;
}
int go(int x, int y, bool lo, bool di, bool sy) {
  if (x >= n) {
    if (lo && di && sy)
      return 0;
    else
      return (1 << 30);
  }
  if (mem[x][y][lo][di][sy] != -1) return mem[x][y][lo][di][sy];
  mem[x][y][lo][di][sy] = (1 << 30);
  for (int i = 0; i < m; i++) {
    mem[x][y][lo][di][sy] =
        min(mem[x][y][lo][di][sy],
            dis(0, i) + go(x + 1, i, lo | low(pass[x][i]), di | dig(pass[x][i]),
                           sy | sym(pass[x][i])));
  }
  return mem[x][y][lo][di][sy];
}
int main() {
  cin >> n >> m;
  int i;
  for (i = 0; i < n; i++) cin >> pass[i];
  memset(mem, -1, sizeof mem);
  cout << go(0, 0, 0, 0, 0) << endl;
  return 0;
}
