#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, o = 0, c = 0;
  cin >> n;
  cin.ignore();
  string s;
  cin >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == '(')
      o++;
    else
      c++;
  if (o != c) {
    cout << "-1";
    return 0;
  }
  long long arr[n], ans = 0, cnt = 0, pos = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == ')')
      cnt++;
    else
      cnt--;
    arr[i] = cnt;
  }
  while (pos < n) {
    long long x = 0;
    if (arr[pos] > 0) {
      for (int i = pos + 1; i < n; i++)
        if (arr[i] == 0) {
          x = i - pos + 1;
          break;
        }
      ans += x;
    }
    if (x == 0)
      pos++;
    else
      pos += x;
  }
  cout << ans;
}
