#include <bits/stdc++.h>
using namespace std;
const int Max = 2e5 + 10;
struct Node {
  int mn, mx, ab, ba, aba;
};
Node merge(Node a, Node b) {
  Node c;
  c.mn = min(a.mn, b.mn);
  c.mx = max(a.mx, b.mx);
  c.ab = max(max(a.ab, b.ab), a.mx - 2 * b.mn);
  c.ba = max(max(a.ba, b.ba), b.mx - 2 * a.mn);
  c.aba = max(max(a.aba, b.aba), max(a.ab + b.mx, a.mx + b.ba));
  return c;
}
int h[Max];
Node seg[Max * 4];
int lazy[Max * 4];
int n;
void put(int x, int idx) {
  seg[idx].mn = seg[idx].mn + x;
  seg[idx].mx = seg[idx].mx + x;
  seg[idx].ab = seg[idx].ab - x;
  seg[idx].ba = seg[idx].ba - x;
  lazy[idx] += x;
}
void shift(int idx) {
  if (lazy[idx] == 0) return;
  put(lazy[idx], idx * 2);
  put(lazy[idx], idx * 2 + 1);
  lazy[idx] = 0;
}
void build(int tl = 0, int tr = 2 * n - 1, int idx = 1) {
  if (tr - tl == 1) {
    seg[idx] = Node{h[tl], h[tl], -h[tl], -h[tl], 0};
    return;
  }
  int mid = (tl + tr) / 2;
  build(tl, mid, idx * 2);
  build(mid, tr, idx * 2 + 1);
  seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
}
void upd(int l, int r, int x, int tl = 0, int tr = 2 * n - 1, int idx = 1) {
  if (l <= tl && tr <= r) {
    put(x, idx);
    return;
  }
  if (r <= tl || tr <= l) return;
  int mid = (tl + tr) / 2;
  shift(idx);
  upd(l, r, x, tl, mid, idx * 2);
  upd(l, r, x, mid, tr, idx * 2 + 1);
  seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '(')
      h[i + 1] = h[i] + 1;
    else
      h[i + 1] = h[i] - 1;
  build();
  cout << '\n';
  cout << seg[1].aba << '\n';
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    if (l > r) swap(l, r);
    if (s[l] != s[r]) {
      if (s[l] == '(')
        upd(l + 1, r + 1, -2);
      else
        upd(l + 1, r + 1, 2);
      swap(s[l], s[r]);
    }
    cout << seg[1].aba << '\n';
  }
}
