#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  deque<char> q;
  int res = 0;
  for (int(i) = (0); i < (n); i++) {
    if (s[i] == '(') {
      if (!q.empty())
        if (q.front() == ')') {
          res += 2;
          q.pop_front();
        } else
          q.push_back(s[i]);
      else
        q.push_back(s[i]);
    } else {
      if (!q.empty())
        if (q.back() == '(')
          q.pop_back();
        else
          q.push_back(s[i]);
      else
        q.push_back(s[i]);
    }
  }
  if (q.empty())
    cout << res;
  else
    cout << -1;
  return 0;
}
