#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (n % 2 != 0)
    cout << -1 << "\n";
  else {
    int count = 0, t = 0, c = 0;
    bool ctr = 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(')
        count++;
      else if (s[i] == ')')
        count--;
      if (count < 0)
        ctr = 1;
      else if (count > 0)
        ctr = 0;
      if (ctr) c++;
      if (ctr && count == 0) {
        t += c;
        ctr = 0;
        c = 0;
      }
    }
    if (count == 0)
      cout << t << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}
