#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string s;
  cin >> n >> s;
  if (n % 2 == 1) {
    cout << -1 << '\n';
    return 0;
  }
  int bal = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(')
      ++bal;
    else
      --bal;
  }
  if (bal != 0) {
    cout << -1 << '\n';
    return 0;
  }
  int ans = 0;
  bool can = false;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(')
      ++bal;
    else
      --bal;
    if (bal < 0)
      can = true;
    else if (bal > 0)
      can = false;
    else if (can) {
      ans += i - j + 1;
      j = i + 1;
    } else {
      j = i + 1;
    }
  }
  cout << ans;
  return 0;
}
