#include <bits/stdc++.h>
using namespace std;
long long i, j, k, l, n, m, res, pnt;
string s;
long long vis[1000009], pref[1000009];
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n >> s;
  for (i = 0; i < s.length(); i++) {
    k += s[i] == '(';
    if (s[i] == '(') q.push(i);
    k -= s[i] == ')';
  }
  if (k != 0) return cout << -1 << endl, 0;
  for (i = 0; i < s.length(); i++) {
    while (!q.empty() && q.top() < i) q.pop();
    if (k == 0) {
      l = q.top();
      q.pop();
      if (l != i) {
        pref[i]++;
        pref[l + 1]--;
      }
      vis[l] = 1;
      k++;
    } else {
      if (q.top() == i)
        k++;
      else
        k--;
    }
  }
  for (i = 0; i < s.length(); i++) {
    if (i != 0) pref[i] += pref[i - 1];
    if (pref[i] != 0) res++;
  }
  cout << res << endl;
  exit(0);
}
