#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  string s;
  cin >> s;
  if (t % 2 != 0) {
    cout << -1 << "\n";
  } else {
    stack<int> s1;
    s1.push(-1);
    int count = 0;
    for (int i = 0; i < t; i++) {
      if (s1.top() == -1) {
        s1.push(s[i]);
      } else if (s1.top() == '(') {
        if (s[i] == '(') {
          s1.push(s[i]);
        } else if (s[i] == ')') {
          s1.pop();
        }
      } else if (s1.top() == ')') {
        if (s[i] == ')') {
          s1.push(s[i]);
        } else if (s[i] == '(') {
          s1.pop();
          count = count + 2;
        }
      }
    }
    if (s1.size() > 1) {
      cout << -1;
    } else if (s1.size() == 1) {
      cout << count;
    }
  }
}
