#include <bits/stdc++.h>
using namespace std;
struct Node {
  int sum = 0;
  pair<int, int> a, b;
  pair<int, pair<int, int>> ab, bc;
  pair<int, tuple<int, int, int>> abc;
  Node() {}
  Node(int val, int pos) {
    sum = val;
    a = {val, pos};
    b = {val, pos};
    ab = {val - 2 * val, {pos, pos}};
    bc = {-2 * val + val, {pos, pos}};
    abc = {val - 2 * val + val, {pos, pos, pos}};
  }
  friend Node Join(const Node &l, const Node &r) {
    Node ret;
    ret.sum = l.sum + r.sum;
    ret.a = max(l.a, {l.sum + r.a.first, r.a.second});
    ret.b = min(l.b, {l.sum + r.b.first, r.b.second});
    ret.ab =
        max({l.ab,
             {l.sum - 2 * l.sum + r.ab.first, r.ab.second},
             {l.a.first - 2 * (l.sum + r.b.first), {l.a.second, r.b.second}}});
    ret.bc =
        max({l.bc,
             {-2 * l.sum + l.sum + r.bc.first, r.bc.second},
             {-2 * l.b.first + l.sum + r.a.first, {l.b.second, r.a.second}}});
    ret.abc = max({l.abc,
                   r.abc,
                   {l.ab.first + l.sum + r.a.first,
                    {l.ab.second.first, l.ab.second.second, r.a.second}},
                   {l.a.first - 2 * l.sum + l.sum + r.bc.first,
                    {l.a.second, r.bc.second.first, r.bc.second.second}}});
    return ret;
  }
};
struct SegmTree {
  int n;
  vector<Node> t;
  SegmTree(const vector<int> &v) : n(v.size()), t(4 * n) {
    Build(0, 0, n - 1, v);
  }
  void Build(int node, int left, int right, const vector<int> &v) {
    if (left == right) {
      t[node] = Node(v[left], left);
      return;
    }
    int mid = (left + right) / 2;
    Build(2 * node + 1, left, mid, v);
    Build(2 * node + 2, mid + 1, right, v);
    t[node] = Join(t[2 * node + 1], t[2 * node + 2]);
  }
  void Update(int node, int left, int right, int pos, int val) {
    if (left == right) {
      t[node] = Node(val, left);
      return;
    }
    int mid = (left + right) / 2;
    if (pos <= mid)
      Update(2 * node + 1, left, mid, pos, val);
    else
      Update(2 * node + 2, mid + 1, right, pos, val);
    t[node] = Join(t[2 * node + 1], t[2 * node + 2]);
  }
  void Update(int pos, int val) { Update(0, 0, n - 1, pos, val); }
  int Get() {
    assert(t[0].sum == 0);
    return t[0].abc.first;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  cin.get();
  string s;
  getline(cin, s);
  vector<int> v(2 * n - 2);
  for (int i = 0; i < 2 * n - 2; ++i) {
    v[i] = s[i] == '(' ? +1 : -1;
  }
  SegmTree st(v);
  cout << st.Get() << '\n';
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    swap(s[x], s[y]);
    st.Update(x, s[x] == '(' ? +1 : -1);
    st.Update(y, s[y] == '(' ? +1 : -1);
    cout << st.Get() << '\n';
  }
}
