#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  char a[4] = {'A', 'C', 'T', 'G'};
  char b[4];
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < 4; i++) {
    int x = abs(b[i] - a[i]);
    int y = 'Z' - b[i] + a[i] - 'A' + 1;
    ans += min(x, y);
  }
  for (int i = 4; i < n; i++) {
    for (int k = 0; k < 3; k++) b[k] = b[k + 1];
    cin >> b[3];
    int temp = 0;
    for (int j = 0; j < 4; j++) {
      int x = abs(b[j] - a[j]);
      int y = 26 - x;
      temp += min(x, y);
    }
    if (temp < ans) ans = temp;
  }
  cout << ans << endl;
}
