#include <bits/stdc++.h>
using namespace std;
constexpr char OB = '(', CB = ')';
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long ans = 0;
  long long bracket_stack = 0;
  long long pos = 0;
  for (long long i = 0; i < n; i++) {
    if (bracket_stack == 0 && s[i] == CB) pos = i;
    if (bracket_stack == -1 && s[i] == OB) {
      ans += i - pos + 1;
    }
    if (s[i] == OB) {
      bracket_stack++;
    } else {
      bracket_stack--;
    }
  }
  cout << (bracket_stack == 0 ? ans : -1) << endl;
}
