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1
3 1
2 0
 2
1 0
xdkh5a 1
 6
1 1
3 0
2 1
6 1
4 0
5 0
 3
1 1
c9qyld 1
3 1
 6
1t68ks 1
pkbj1g 1
5 0
5pw8wm 1
1 0
4 0
 2
1 0
2 1
 3
2 1
1 0
3 0
 5
73s1nt 1
sbngv2 0
4n3qri 1
byhzp8 1
adpjs4 0
 2
1 1
2 1
 2
1 0
2 1
 5
2 0
3 1
4 0
1 1
5 1
 5
5 0
ts7a1c 0
4 1
1 1
2 1
 4
4 0
pa613p 1
uuizq7 1
2 0
 2
17dgbb 0
2 1
 6
p1wjw9 1
ueksby 0
u1ixfc 1
j3lk2e 1
36iskv 0
9imqi1 0
 2
kfsipl 0
1jj1ol 0
 4
4 1
wgh8s0 1
1 0
2 1
 3
2 1
1 0
omitxh 1
 3
2 1
1 0
3 0

2
move zwfnx2 2
move 7g8t6z 3
 2
move qmf7iz 2
move djwdce 3
 4
move 4 2
move 1 4
move 410jiy 1
move xc98l2 3
 1
move hs9j9t 1
 4
move 5 2
move 1 4
move 1n9mqv 1
move xhfzge 5
 3
move 3 4
move 1 3
move 4 1
 5
move w0g96a 1
move v99tdi 3
move mywrle 4
move weh22w 2
move 9hywt4 5
 6
move 4 1
move 5 2
move 7l3kg1 3
move oclx1h 4
move q25te0 5
move rdm6fu 6
 3
move bxo0pe 1
move bt50pa 2
move 2tx68t 3
 5
move 5 1
move 949pnr 2
move 9sxhcr 5
move x8srx3 3
move cl7ppd 4
 3
move 4 6
move 2 4
move 6 2
 4
move 3 4
move iipymv 1
move vakd5b 2
move 2ktczv 3
 1
move t0dfz3 2
 3
move 3 4
move 1 3
move 4 1
 3
move 2 3
move uv8c54 1
move 508bb0 2
 3
move 4 5
move 1 4
move 5 1
 1
move abbdf7 1
 0
 3
move 4 1
move 3 4
move hex0ur 3
 4
move 2kk04q 3
move kdktvk 1
move c4i5k8 2
move awaock 4
 3
move 3 4
move 1 3
move 4 1
 3
move 1 3
move 2 1
move 3 2
 5
move qp7q8l 1
move wglqu8 2
move 0zluka 4
move 9i7kta 5
move nwf8m3 6
 3
move 3 4
move 1 3
move 4 1
 3
move 2 4
move 1 2
move 4 1
 3
move 1 2
move fr4033 1
move sc7czx 4
 5
move 1 5
move 4 1
move 2 4
move 3 2
move 5 3
 4
move 4 1
move 4i2i2a 4
move pf618n 2
move lx6nmh 3
 1
move przvln 1
 3
move 3 1
move s0bdnf 3
move y9144q 4
 5
move puusew 1
move pvoy4h 2
move wdzx4r 3
move 1z84cx 4
move ozsuvd 5
 5
move 2 4
move 7ajg8o 1
move p7cqxy 2
move 3qrp34 5
move h93m07 3
 3
move 4 6
move 2 4
move 6 2
 4
move 12qcjd 1
move uthzbz 3
move b3670z 4
move l2u93o 2
 3
move 4 1
move aio11n 4
move hvshea 5
 4
move 4 2
move 1 4
move 73xpqz 1
move xpteku 3
 3
move 4 5
move 2 4
move 5 2
 2
move t4hdos 1
move dhje0g 2
 4
move 4 1
move u9do88 2
move 787at9 3
move fcud6k 4
 6
move 5 1
move 2 4
move 3 5
move 6qb4ia 2
move w9h0pv 3
move q92z4i 6
 3
move 2 5
move 4 2
move 5 4
 4
move 2 3
move mqbjos 4
move 6mhijg 1
move 6wum8y 2
 4
move 5 3
move eik3kw 4
move zoonoj 5
move ivzfie 6
 3
move 4 2
move 3 4
move 2y4agr 3
 3
move 4 5
move 2 4
move 5 2
 0
 5
move 4 1
move 2 3
move setddt 2
move 5sn77g 4
move bz16cb 5
 4
move 3 2
move noidnv 3
move x3xiiz 4
move 1lfa9v 5
 3
move n3pmj8 2
move 2alui6 3
move e7lf4u 1
 3
move 1 2
move 6a2egk 1
move dr1lp8 3
 5
move 5 1
move 3 4
move wje9ts 2
move kytn5q 3
move 7frk8z 5
 5
move 1 5
move 3 1
move 2 3
move 4 2
move 5 4
 3
move 2x7a4g 1
move 27lqe6 2
move zfo3sp 3
 3
move 3 7
move 6 3
move 7 6
 4
move 3 4
move q4b449 1
move cjg1x2 2
move e878er 3
 2
move iy1dq6 2
move wy6i6o 4
 3
move 1 3
move 2 1
move 3 2
 2
move 2 4
move g6ugrm 2
 5
move 1 5
move 4 1
move 2 4
move 3 2
move 5 3
 4
move 3 1
move v04v6j 2
move 1o0bp2 3
move 9tn379 4
 3
move 2 5
move 4 2
move 5 4
 3
move 1 3
move 2 1
move 3 2
 5
move 1 4
move 2 5
move p2gcxf 1
move wfyoiq 2
move zjw3vg 3
 3
move 2 4
move x2miqh 2
move 1rq643 5
 2
move o9z069 1
move 5hools 2
 3
move 2 6
move 5 2
move 6 5
 16
move 16 1
move 18 2
move 5 10
move 6 12
move 1pon1p 3
move e14bkx 4
move php8vy 5
move sfms2u 6
move d9stsx 13
move f2548d 14
move jvpwuo 15
move keeona 16
move sbklx4 18
move wzf4eb 19
move yszsig 20
move yyf4q2 7
 2
move 4 2
move d1cks2 4
 1
move t6kdte 1
 3
move 4 5
move 2 4
move 5 2
 3
move 4 5
move 1 4
move 5 1
 3
move 2 5
move 4 2
move 5 4
 0
 3
move 4 5
move 1 4
move 5 1
 2
move joa6mr 1
move 5xzjm4 2
 3
move z1nwrd 1
move t0xrja 2
move 106qy1 3
 5
move 1 5
move 4 1
move 2 4
move 3 2
move 5 3
 3
move 1 4
move yumiqt 1
move t19jus 2
 2
move 9afh0z 1
move 0qcaht 2
 3
move 4 5
move 1 4
move 5 1
 4
move 1 4
move y0wnwz 1
move 0totai 2
move ym8xwz 3
 5
move 5 3
move 2 4
move 6 2
move 7igwk9 5
move dx2yu0 6
 3
move 4 2
move 3 4
move 4soxj3 3
 4
move 1 4
move 2 5
move h015vv 1
move 9w2keb 2
 3
move j9rnac 1
move oetwfz 2
move d6n3ww 3
 3
move r2qlj2 1
move t8wf1y 2
move igids8 3
 0
 2
move yzzyab 1
move 728oq0 2
 3
move 3 4
move 1 3
move 4 1
 4
move 1h3t85 1
move rf2ikt 4
move 3vhl6e 2
move 5l3oka 6
 2
move vsyajx 3
move 783b38 5
 1
move i19lnk 1
 3
move 4 2
move m5nhux 4
move ogvgi7 5
 5
move 1 5
move 3 1
move 2 3
move 4 2
move 5 4
 2
move 3 2
move av5vex 3
 4
move ir7oz8 1
move vj4v5t 2
move kwkahb 3
move j5s8o1 4
 4
move ynagvf 1
move ojz4mm 2
move dovec3 4
move nc1jye 5
 4
move 1 2
move 3 1
move 3a5ad1 3
move nj94jx 4
 5
move 5 3
move 1 4
move wrrcmu 1
move 3r4wqz 5
move p4tuyt 6
 5
move 1 5
move 4 1
move 2 4
move 3 2
move 5 3
 3
move 2 6
move 5 2
move 6 5
 2
move c8p28p 1
move vk4gdf 3
 2
move qvw4ow 2
move ne0ng9 4
 1
move 01 1
 5
move 1 5
move 3 1
move 2 3
move 4 2
move 5 4
 0
 4
move 3 1
move 2 3
move 4 2
move cvfl8i 4
 2
move dbif39 1
move e8dkf8 2
 3
move 3 1
move scrn8k 2
move ycvm9s 3
 5
move 1 3
move 2 4
move 0jc7xb 1
move 1m7l9s 5
move 9xzkau 2
 3
move 2 6
move 5 2
move 6 5
 3
move 4 6
move 2 4
move 6 2
 2
move qy2kmc 1
move qb4crj 2
 0
 4
move 3 2
move 1 3
move 4 1
move mod9zl 4
 3
move 2 4
move 1 2
move 4 1
 2
move 02 1
move 01 2
 2
move 7ph5fw 2
move tfxz1j 3
 1
move xzp9ni 1
 1
move sd84r7 1
 3
move 1 3
move 2 1
move 3 2
 3
move 3 7
move 6 3
move 7 6
 1
move gdm5ri 3
 1
move cxbbpd 2
 1
move vinxur 2
 6
move 2 4
move 6slonw 1
move ptk9mc 2
move 57a4nq 5
move hiq2f7 3
move c0gtv3 6
 3
move 4 6
move 2 4
move 6 2
 3
move af2f6j 1
move mjni5l 2
move jvyxgc 3
 5
move 4 2
move 1 4
move 3 5
move log9cm 1
move u5m0ls 3
 3
move 3 4
move 1 3
move 4 1
 3
move 1 3
move 2 1
move 3 2
 3
move 9f4aoa 1
move f4m1ec 2
move qyr2h6 3
 3
move 2 5
move 4 2
move 5 4
 5
move 5 1
move e8rwo4 2
move gz6dhj 3
move rfpjp6 4
move z9zr7d 5
 1
move jkwekx 1
 4
move yi9ta0 1
move meljgm 2
move f7bqon 3
move 5bbvun 4
 2
move orwsz0 1
move mbt097 2
 3
move zhrmyb 1
move 0la3gu 2
move 3iprc0 4
 3
move 1 4
move 1wp56i 1
move 6m76jb 3
 1
move 26mp5s 2
 3
move 4 3
move 2 4
move q4hyeg 2
 3
move 5nvzu4 1
move vkpzzk 2
move zamzcz 3
 2
move 1 3
move izfotg 1
 0
 3
move 3 7
move 6 3
move 7 6
 5
move 1 5
move 3 1
move 2 3
move 4 2
move 5 4
 4
move uilh9a 4
move 4lxxh9 1
move kqdpzy 2
move n1d7hd 3
 0
 3
move 3 4
move 1 3
move 4 1
 2
move ejo0a4 2
move gqzdbk 5
 0
 1
move 1qe46n 2
 5
move 2 4
move cbhvyf 1
move l1z5mg 6
move wkwhby 2
move x7fdh9 3
 3
move 1 3
move 2 1
move 3 2
 3
move 3 6
move 5 3
move c3py3h 5
 4
move 4 1
move kgw83p 2
move p3p3ch 3
move 0te9lv 4
 4
move 5 1
move 5sbtul 3
move 8i2duz 4
move 4b85z6 5
 3
move 3 4
move 1 3
move 4 1
 0
 1
move g5jlzp 2
 3
move donjp9 1
move unw560 2
move 0iswxk 3
 3
move 5 2
move 1 5
move hmpfsz 1
 3
move 4 5
move 2 4
move 5 2
 2
move 1 2
move xdkh5a 1
 3
move 3 7
move 6 3
move 7 6
 1
move c9qyld 2
 4
move 1 6
move 1t68ks 1
move pkbj1g 2
move 5pw8wm 3
 3
move 1 3
move 2 1
move 3 2
 3
move 2 4
move 1 2
move 4 1
 5
move 73s1nt 1
move sbngv2 4
move 4n3qri 2
move byhzp8 3
move adpjs4 5
 0
 3
move 1 3
move 2 1
move 3 2
 3
move 2 6
move 5 2
move 6 5
 2
move 4 3
move ts7a1c 4
 3
move 2 3
move pa613p 1
move uuizq7 2
 2
move 2 1
move 17dgbb 2
 6
move p1wjw9 1
move u1ixfc 2
move j3lk2e 3
move ueksby 4
move 36iskv 5
move 9imqi1 6
 2
move kfsipl 1
move 1jj1ol 2
 3
move 4 3
move 1 4
move wgh8s0 1
 2
move 1 3
move omitxh 1
 3
move 2 4
move 1 2
move 4 1

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
char a[N][9];
int ty[N];
int n;
int e;
bool hav[N];
int m = 0;
int q1[N];
int q2[N];
int ans;
int fr[N], to[N];
int get(int x) {
  int len = strlen(a[x] + 1);
  if (a[x][1] == '0') return 0;
  int w = 0;
  for (int i = (int)1; i <= (int)len; i++) {
    if (a[x][i] <= '9' && a[x][i] >= '0') {
      w *= 10;
      w += (a[x][i] - '0');
    } else
      return 0;
  }
  if (w > n) w = 0;
  return w;
}
void move(int a, int b) {
  if (a <= n && a >= 1) hav[a] = 0;
  if (b <= n && b >= 1) hav[b] = 1;
  ++ans;
  fr[ans] = a;
  to[ans] = b;
}
int main() {
  scanf("%d", &n);
  for (int i = (int)1; i <= (int)n; i++) {
    scanf("%s", a[i] + 1);
    scanf("%d", &ty[i]);
    e += ty[i];
  }
  int kg = 0;
  for (int i = (int)1; i <= (int)n; i++) {
    int w = get(i);
    if (w && w <= n) {
      if (w <= e && ty[i]) {
        hav[w] = 1;
        ++m;
      }
      if (w > e && w <= n && (ty[i] == 0)) {
        hav[w] = 1;
        ++m;
      }
      if (w <= e && ty[i] == 0) {
        q1[++q1[0]] = i;
        hav[w] = 1;
      }
      if (w > e && w <= n && ty[i] == 1) {
        q2[++q2[0]] = i;
        hav[w] = 1;
      }
    } else {
      kg = i;
    }
  }
  if (m == n) {
    printf("0\n");
    return 0;
  }
  if (!kg) {
    assert(q1[0] == q2[0]);
    move(get(q1[1]), n + 1);
    move(get(q2[1]), get(q1[1]));
    for (int i = (int)2; i <= (int)q1[0]; i++) {
      move(get(q1[i]), get(q2[i - 1]));
      move(get(q2[i]), get(q1[i]));
    }
    move(n + 1, get(q2[q2[0]]));
  } else {
    if ((q1[0] > q2[0]) || (q1[0] == q2[0] && e < n - e)) {
      for (int i = (int)e + 1; i <= (int)n; i++)
        if (!hav[i]) kg = i;
      for (int i = (int)q2[0]; i >= (int)1; i--) {
        move(get(q2[i]), get(q1[q1[0]]));
        move(get(q1[q1[0]]), kg);
        hav[kg] = 1;
        q1[0]--;
        kg = get(q2[i]);
      }
      if (q1[0]) {
        move(get(q1[q1[0]--]), kg);
      } else
        hav[kg] = 0;
      for (int i = (int)e + 1; i <= (int)n; i++)
        if (!hav[i]) {
          if (q1[0]) move(get(q1[q1[0]--]), i);
        }
    } else {
      for (int i = (int)1; i <= (int)e; i++)
        if (!hav[i]) kg = i;
      for (int i = (int)q1[0]; i >= (int)1; i--) {
        move(get(q1[i]), get(q2[q2[0]]));
        move(get(q2[q2[0]]), kg);
        hav[kg] = 1;
        q2[0]--;
        kg = get(q1[i]);
      }
      if (q2[0]) {
        move(get(q2[q2[0]--]), kg);
      } else
        hav[kg] = 0;
      for (int i = (int)1; i <= (int)e; i++)
        if (!hav[i]) {
          if (q2[0]) move(get(q2[q2[0]--]), i);
        }
    }
    int t = e + 1;
    for (int i = (int)1; i <= (int)n; i++)
      if (!ty[i])
        if (!(get(i) && get(i) <= n)) {
          while (hav[t]) ++t;
          move(-i, t);
          hav[t] = 1;
        }
    t = 1;
    for (int i = (int)1; i <= (int)n; i++)
      if (ty[i])
        if (!(get(i) && get(i) <= n)) {
          while (hav[t]) ++t;
          move(-i, t);
          hav[t] = 1;
        }
  }
  printf("%d\n", ans);
  for (int i = (int)1; i <= (int)ans; i++) {
    printf("move ");
    if (fr[i] > 0)
      printf("%d ", fr[i]);
    else
      printf("%s ", a[-fr[i]] + 1);
    if (to[i] > 0)
      printf("%d\n", to[i]);
    else
      printf("%s\n", a[-to[i]] + 1);
  }
  return 0;
}
