VAR A,B,C,D,G:ARRAY[1..100]OF INTEGER;
    M,N,I,J,L,DEM,T,K:INTEGER;
    F:TEXT;
BEGIN
     WRITE('NHAP N:= ');READLN(N); WRITELN;
     FOR I:=1 TO N DO
     BEGIN
          WRITE('A[',I,']= ');
          READLN(A[I]);
     END; WRITELN;
     WRITE('NHAP M:= ');READLN(M); WRITELN;
     FOR I:=1 TO M DO
     BEGIN
          WRITE('B[',I,']= ');
          READLN(B[I]);
     END; WRITELN;
     ASSIGN(F,'BAI3.OUT');REWRITE(F);
     DEM:=0;T:=1;K:=1;
     FOR I:=1 TO N DO
     BEGIN
          FOR J:=1 TO M DO
          IF A[I]=B[J] THEN
          BEGIN
               DEM:=DEM+1;
               C[T]:=I;
               D[K]:=J;
               T:=T+1;
               K:=K+1;
          END;
     END;
     WRITE(F,DEM);WRITELN(F);
     FOR I:=1 TO N DO
     BEGIN
          FOR J:=1 TO M DO
          IF A[I]=B[J] THEN WRITE(F,A[I]:3);
     END;
     WRITELN(F);
     FOR I:=1 TO DEM DO WRITE(F,C[I]:3);
     WRITELN(F);
     FOR I:=1 TO DEM DO WRITE(F,D[I]:3);
     CLOSE(F);
END.
