#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long mod = 1000000007;
const int MX = 0x3f3f3f3f;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i;
  cin >> n;
  string s;
  cin >> s;
  stack<int> br;
  int op = 0, cl = 0;
  int j = 0, ans = 0;
  for (i = 0; i <= n - 1; i++) {
    if (s[i] == '(')
      br.push(s[i]), op++;
    else {
      if (!br.empty()) br.pop();
      cl++;
    }
    j++;
    if (op == cl && br.empty())
      op = 0, cl = 0, j = 0;
    else if (op == cl) {
      ans += j;
      while (!br.empty()) br.pop();
      j = 0;
      op = 0;
      cl = 0;
    }
  }
  if (!br.empty() || cl)
    cout << -1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
