#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e6 + 10;
int arr[maxx + 5];
int main() {
  int n, ans = 0, temp = 0, l = 0, r = 0, last = -1;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      l++;
    } else {
      r++;
    }
    if (i == 0) {
      if (s[0] == '(') {
        arr[0] = 1;
      } else {
        arr[0] = -1;
      }
      continue;
    }
    if (s[i] == '(') {
      arr[i] = arr[i - 1] + 1;
    } else {
      arr[i] = arr[i - 1] - 1;
    }
  }
  if (l != r) {
    cout << "-1" << endl;
    return 0;
  }
  arr[n] = 123;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] == 0) {
      if (arr[i + 1] < 0) {
        ans += (last - i);
      }
      last = i;
    }
  }
  if (arr[0] < 0) {
    ans += (last + 1);
  }
  cout << ans << endl;
  return 0;
}
