#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
int MOD = (int)1e9 + 7;
const int N = (int)1000;
string s;
bool valid(int i, int j) {
  stack<int> st;
  for (int k = i; k <= j; ++k) {
    if (s[k] == ')') {
      if (st.empty()) return false;
      st.pop();
    } else
      st.push(k);
  }
  if (st.empty())
    return true;
  else
    return false;
}
int main() {
  int n;
  cin >> n;
  cin >> s;
  int o = 0, c = 0;
  for (auto x : s)
    if (x == '(')
      o++;
    else
      c++;
  if (o != c) return puts("-1");
  o = 0, c = 0;
  int ans = 0;
  for (int i = 0, j = 0; j < int((s).size()); j++) {
    if (s[j] == '(')
      o++;
    else
      c++;
    if (o == c) {
      if (!valid(i, j)) ans += j - i + 1;
      i = j + 1;
      o = 0, c = 0;
    }
  }
  cout << ans << endl;
}
