#include <bits/stdc++.h>
using namespace std;
long long int n, i, k, l, r, Count, ans, Prev, prevCount;
string s;
int main() {
  cin >> n;
  cin >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '(')
      l++;
    else if (s[i] == ')')
      r++;
  }
  if (l != r) {
    cout << -1 << endl;
    return 0;
  }
  Count = 0;
  Prev = -1;
  for (i = 0; i < n; i++) {
    if (s[i] == ')')
      Count--;
    else if (s[i] == '(')
      Count++;
    if (Count > 0 || (Count == 0 && prevCount > 0)) Prev = i;
    if (prevCount < 0 && Count == 0) {
      ans += (i - Prev);
      Prev = i;
    }
    prevCount = Count;
  }
  cout << ans << endl;
  return 0;
}
