#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  bool flag = true;
  int cnt = 0;
  for (char c : s) {
    if (c == '(') {
      cnt++;
    } else {
      cnt--;
    }
  }
  if (cnt != 0) {
    flag = false;
  }
  if (!flag) {
    cout << "-1";
    return 0;
  }
  int last = -1;
  cnt = 0;
  long long ans = 0;
  flag = true;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      cnt++;
    } else {
      cnt--;
    }
    if (cnt == 0 && !flag) {
      flag = true;
      ans += i - last;
    }
    if (cnt == 0) {
      last = i;
    }
    if (cnt < 0) {
      flag = false;
    }
  }
  cout << ans;
  return 0;
}
