#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int n, k = 0, l = 0, p = 0;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    (s[i] == '(') ? k++ : k--;
    if (!k) {
      if (s[i] == '(') l += (i - p + 1);
      p = i + 1;
    }
  }
  cout << ((k) ? -1 : l) << "\n";
  return 0;
}
