#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  char ch;
  long long a = 0, b = 0;
  vector<long long> arr;
  arr.push_back(0);
  for (long long i = 0; i < n; i++) {
    cin >> ch;
    if (ch == '(')
      b++;
    else
      a++;
    arr.push_back(b - a);
  }
  if (a != b)
    cout << "-1";
  else {
    long long ans = 0, k = 0;
    for (long long i = 1; i < arr.size(); i++) {
      if (arr[i] == 0) {
        if (arr[i - 1] < 0) {
          ans += k + 1;
        }
        k = 0;
      } else {
        k++;
      }
    }
    cout << ans;
  }
  return 0;
}
