#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
using namespace std;
const int maxm=1e5+5;
int in[maxm];
int out[maxm];
int n;
//想了好久才发现题目以及说了是傻逼二维偏序，可恶
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>in[i];//in[i]表示进入时间为i的车的id
    }
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        out[x]=i;//out[i]表示车i出去的时间
    }
    int ans=0;
    int ma=0;
    for(int i=1;i<=n;i++){
        ma=max(ma,out[in[i]]);//1-i出车时间最早的，假设为j
        if(ma>out[in[i]]){//如果j<i但out[j]>out[i]  ans++
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
/*
 *                             UM.
 *                            J@B@1                                                                    iO@1
 *                           Y@@@B@BB.                                                              7B@B@B@
 *                          :@B@i,B@B@O                                                          ,Z@B@B@B@Br
 *                          @B@q   i@B@BS                                                      7@B@@@O5vMB@q
 *                         8@@B      LB@B@i                                                  FB@@@BNjYjLE@B@
 *                        ,B@B:        0@@@Z                                               P@B@BM1JJ125JPB@B
 *                        B@BB          :@B@B                                            XB@B@Z2LuU52F2u2@B@.
 *                       :@B@             @@@B:                                        v@B@B8uJj51F1525uUB@B7
 *                       @B@O              0@@B.               ..::ir7vvYUuU777r::.   B@B@OULU2F2F151F11Y@B@S
 *                       B@B,               8B@B  :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
 *                      U@B@                 @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
 *                      @B@u                 v@@@@MSur:.                    LB@MvvjJuU5YU252F1F1F25251F2uX@@@
 *                      @@@.                                                N@BML2U2UUU12F15252525251515Jk@@B
 *                     r@B@                                                 YB@Bju52121252515252F15251F2u5@B@
 *                     PB@B                                                  @@@PYUF151F25151F152F2F1F15jF@@B
 *                     @@BS                                                  N@@@UJ2F25252F251525151F1F1u5@B@
 *                     @@@7                                                   B@B@5Yj12F152F1F1F25252515jFB@B
 *                     B@Bi                                                    M@B@O2Luu52525212F151121UY1@B@7
 *                    O@B@:                                                     v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
 *                  7B@B@,                                                        1B@@@B@GPF1uujuu21PNMB@B@B@B@@
 *                 qB@B2                                                            i8B@B@B@B@B@@@@@B@B@B@q: @@@B
 *                MB@B:                                                                 7SBB@B@B@B@B@Zu:      @B@B
 *               ZB@B.                                              ,v.                                        @B@L
 *              LB@B,                         Y7                    @B@Bu                                      7@B@
 *   :B@B@@B2:  @@B7                         @B@Z                   r@B@B@BP:                                   B@BE
 *    BB@@@B@B@B@BE                        r@B@B                       7@B@B@B@Ou:                              iB@B
 *         :uM@@B@@2.           :7::::ivk@B@B@0                           :5B@B@B@B@B@B@G.                       @B@i
 *            BB@@@B@@         :@B@B@@@B@B@@1                                 .i5M@B@B@@@5                       M@@2
 *            B@B ,@B1          L0EZZG0F7:                                            .:,                        uB@MrP@M7
 *           2@B@                                                                                ,O@B@B@B@B
 *           @B@1                                                     :@B@@@r                                :@@@@B@BL:,,
 *           B@Bi                         :2ZS;                      :@B@B@B@r                               L@B@B@BU
 *           @B@.                        @@@B@B@                     vB@B@B@B5                                   @B@i
 *           B@B                        7B@B@B@BM                     OB@B@B@                                   ,B@B
 *           @B@                         @B@B@@@i                       rL7.                                    B@BM
 *           B@B7.:                       NB@@M.                                                               .@B@.
 *  .;JEB@@@B@B@B@B@.                                                                                .  @B@u
 *@@@B@B@B@B@@@B@18U                                                                                :B@B@B@BU,
 *7@BOui.    ,@@B                                                                                SP@B@B@B@B@Or
 *            @@@U                                                                                B@BJ.YO@B@B@i
 *            r@B@                                                                                :B@Bk     .k@B@
 *             B@B@                                                                                LB@@k         2i
 *              B@BM                                      .7jXEGqF7:                                     OB@@L
 *              .B@BM                                   .B@B@B@B@B@B@.                                 :@B@B:
 *               .B@B@                                   @@MYr::ivG@B                                .M@B@G
 *                 B@@@S                                                                           ,MB@B@,
 *                  v@@@BF                                                                      .1B@B@Br
 *                    2@@B@BL                                                                ,FB@@@B8,
 *                      r@B@B@BF,                                                        :YBB@B@B@B
 *                         L@B@B@B@P7,                                           .ivXB@B@B@B@B@M@B@
 *                            ,1B@B@B@B@@@BOP2L7i:,.              ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
 *                              @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
 *                              B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
 *                              @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
 *                              B@@X::,::::iirir;riri:E@B@1         ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
 *                              @@@B@BBq5v7ii:::::::.2@@@i  ..,..     @B@@,,:::::irv2XMB@B@B@B@2@B@:
 *                             .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,,  :.   @B@P50MB@B@B@B@B@@@BS:   @@B1
 *                             E@B@   ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57.        kB@B
 *                            .@B@:          .,ivu5Nq@B@u  ..,.. SB@B@@@B@PL7i,                 ,@B@
 *                            @@@8                   i@B@:    .     :B@B@@                       B@@2
 *                          i@@@@                     0@B@u          B@@B.                       vB@B
 *                         ,@B@G                       L@B@BOv:.:iFB@B@M                          @B@Bi
 *                          vNi                          S@@B@B@B@B@BM:                            MB@N
 *                                                          758BMqJ,
 *
 *                 .  YO.               vq                            :G       Z:
 *        SqOMBB@B@Br @@r rBE           @B     B@@@@@B@ONX8k    i::::.OB1.:::.u@O.::::i           @B@B@U:@@B@@BPEBu
 *        B@@NB@k.    5@i  uB@E.        BM     1U2uUJvirB@@Z   r@@B@B@@@B@B@B@B@B@@@B@Bi   LB@B@1 BX :@k uLLLvr@BJ:
 *            iB      iBi    7@     .@M8@BGMZZ         @@F            ,B       Pi          v@  Bq @i v@        B@
 *       vuL7r8@S7vJL7N@Z7LLri;72.   F7@Bvvv@@       @BX         @@@B@B@@@@@B@@@B@B@B      7@  @F Bi @q  @B@Bu @B
 *       N@B@G@@@8@BBOMB@G@BMNXG@,     B@   @@      .Bk          .:u;    i@:      Zv       7@  Bk @,;@  ,BY @B B@
 *            r@       @G     5.      ,@v   BZ :::,.r@E .::i,      @B     B@    .@BL       7@  @F B:i@. .@  @M @B
 *            7B: ,vO, @@   iB@:      @B   7@:MB@B@B@@@B@B@BM       @@.    B:  2@q         7@  BS @i 0@  B. @O B@
 *       ,r2EBB@B@B@Bi G@  @BB        B@   @B        @S              :    r@   ..          7B  @F @7  B7 @  @B @B
 *       E@B@UOBr       @B@Bi          L@0PB        .BZ        .@B@B@B@B@B@B@B@B@B@B@B@,   r@  BF @i  @G B@B@B B@
 *            7@,      kB@U    ;r        @@@.       .@Z                GBuL@iBBi           vB@B@q BP:5@7 @u,.  @B
 *            LBi   YB@BrB@    @@       @B:L@Br      BM             .M@B  rB  rB@J         v@. Pi @XZ8r  .     B@
 *        .   G@i B@BM.  ,B@,  @B    iB@B    N, 7r..q@k         ,LB@B8    J@,   i@B@B1r           Br           @@
 *        MB@B@B  ,i       B@B@B,    B@:        @B@B@F         .@BB:      P@i      :OBZ          .@U       B@B@B:
 *                          .ll                                           rB.                     :
 */


#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
using namespace std;
const int maxm=1e5+5;
int a[maxm];
int b[maxm];
int mark[maxm];
int n;
struct Node{
    int in,out;
}e[maxm];
bool cmp(Node a,Node b){
    return a.in<b.in;
}
int c[maxm];
int lowbit(int i){
    return i&-i;
}
void add(int i){
    while(i<=n){
        c[i]++;
        i+=lowbit(i);
    }
}
int ask(int i){
    int ans=0;
    while(i){
        ans+=c[i];
        i-=lowbit(i);
    }
    return ans;
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        e[a[i]].in=i;
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
        e[b[i]].out=i;
    }
    sort(e+1,e+1+n,cmp);
    int ans=0;
    for(int i=1;i<=n;i++){
        add(e[i].out);
        int t=i-ask(e[i].out);
        if(t>0)ans++;
    }
    cout<<ans<<endl;
    return 0;
}
/*
 *                             UM.
 *                            J@B@1                                                                    iO@1
 *                           Y@@@B@BB.                                                              7B@B@B@
 *                          :@B@i,B@B@O                                                          ,Z@B@B@B@Br
 *                          @B@q   i@B@BS                                                      7@B@@@O5vMB@q
 *                         8@@B      LB@B@i                                                  FB@@@BNjYjLE@B@
 *                        ,B@B:        0@@@Z                                               P@B@BM1JJ125JPB@B
 *                        B@BB          :@B@B                                            XB@B@Z2LuU52F2u2@B@.
 *                       :@B@             @@@B:                                        v@B@B8uJj51F1525uUB@B7
 *                       @B@O              0@@B.               ..::ir7vvYUuU777r::.   B@B@OULU2F2F151F11Y@B@S
 *                       B@B,               8B@B  :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
 *                      U@B@                 @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
 *                      @B@u                 v@@@@MSur:.                    LB@MvvjJuU5YU252F1F1F25251F2uX@@@
 *                      @@@.                                                N@BML2U2UUU12F15252525251515Jk@@B
 *                     r@B@                                                 YB@Bju52121252515252F15251F2u5@B@
 *                     PB@B                                                  @@@PYUF151F25151F152F2F1F15jF@@B
 *                     @@BS                                                  N@@@UJ2F25252F251525151F1F1u5@B@
 *                     @@@7                                                   B@B@5Yj12F152F1F1F25252515jFB@B
 *                     B@Bi                                                    M@B@O2Luu52525212F151121UY1@B@7
 *                    O@B@:                                                     v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
 *                  7B@B@,                                                        1B@@@B@GPF1uujuu21PNMB@B@B@B@@
 *                 qB@B2                                                            i8B@B@B@B@B@@@@@B@B@B@q: @@@B
 *                MB@B:                                                                 7SBB@B@B@B@B@Zu:      @B@B
 *               ZB@B.                                              ,v.                                        @B@L
 *              LB@B,                         Y7                    @B@Bu                                      7@B@
 *   :B@B@@B2:  @@B7                         @B@Z                   r@B@B@BP:                                   B@BE
 *    BB@@@B@B@B@BE                        r@B@B                       7@B@B@B@Ou:                              iB@B
 *         :uM@@B@@2.           :7::::ivk@B@B@0                           :5B@B@B@B@B@B@G.                       @B@i
 *            BB@@@B@@         :@B@B@@@B@B@@1                                 .i5M@B@B@@@5                       M@@2
 *            B@B ,@B1          L0EZZG0F7:                                            .:,                        uB@MrP@M7
 *           2@B@                                                                                ,O@B@B@B@B
 *           @B@1                                                     :@B@@@r                                :@@@@B@BL:,,
 *           B@Bi                         :2ZS;                      :@B@B@B@r                               L@B@B@BU
 *           @B@.                        @@@B@B@                     vB@B@B@B5                                   @B@i
 *           B@B                        7B@B@B@BM                     OB@B@B@                                   ,B@B
 *           @B@                         @B@B@@@i                       rL7.                                    B@BM
 *           B@B7.:                       NB@@M.                                                               .@B@.
 *  .;JEB@@@B@B@B@B@.                                                                                .  @B@u
 *@@@B@B@B@B@@@B@18U                                                                                :B@B@B@BU,
 *7@BOui.    ,@@B                                                                                SP@B@B@B@B@Or
 *            @@@U                                                                                B@BJ.YO@B@B@i
 *            r@B@                                                                                :B@Bk     .k@B@
 *             B@B@                                                                                LB@@k         2i
 *              B@BM                                      .7jXEGqF7:                                     OB@@L
 *              .B@BM                                   .B@B@B@B@B@B@.                                 :@B@B:
 *               .B@B@                                   @@MYr::ivG@B                                .M@B@G
 *                 B@@@S                                                                           ,MB@B@,
 *                  v@@@BF                                                                      .1B@B@Br
 *                    2@@B@BL                                                                ,FB@@@B8,
 *                      r@B@B@BF,                                                        :YBB@B@B@B
 *                         L@B@B@B@P7,                                           .ivXB@B@B@B@B@M@B@
 *                            ,1B@B@B@B@@@BOP2L7i:,.              ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
 *                              @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
 *                              B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
 *                              @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
 *                              B@@X::,::::iirir;riri:E@B@1         ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
 *                              @@@B@BBq5v7ii:::::::.2@@@i  ..,..     @B@@,,:::::irv2XMB@B@B@B@2@B@:
 *                             .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,,  :.   @B@P50MB@B@B@B@B@@@BS:   @@B1
 *                             E@B@   ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57.        kB@B
 *                            .@B@:          .,ivu5Nq@B@u  ..,.. SB@B@@@B@PL7i,                 ,@B@
 *                            @@@8                   i@B@:    .     :B@B@@                       B@@2
 *                          i@@@@                     0@B@u          B@@B.                       vB@B
 *                         ,@B@G                       L@B@BOv:.:iFB@B@M                          @B@Bi
 *                          vNi                          S@@B@B@B@B@BM:                            MB@N
 *                                                          758BMqJ,
 *
 *                 .  YO.               vq                            :G       Z:
 *        SqOMBB@B@Br @@r rBE           @B     B@@@@@B@ONX8k    i::::.OB1.:::.u@O.::::i           @B@B@U:@@B@@BPEBu
 *        B@@NB@k.    5@i  uB@E.        BM     1U2uUJvirB@@Z   r@@B@B@@@B@B@B@B@B@@@B@Bi   LB@B@1 BX :@k uLLLvr@BJ:
 *            iB      iBi    7@     .@M8@BGMZZ         @@F            ,B       Pi          v@  Bq @i v@        B@
 *       vuL7r8@S7vJL7N@Z7LLri;72.   F7@Bvvv@@       @BX         @@@B@B@@@@@B@@@B@B@B      7@  @F Bi @q  @B@Bu @B
 *       N@B@G@@@8@BBOMB@G@BMNXG@,     B@   @@      .Bk          .:u;    i@:      Zv       7@  Bk @,;@  ,BY @B B@
 *            r@       @G     5.      ,@v   BZ :::,.r@E .::i,      @B     B@    .@BL       7@  @F B:i@. .@  @M @B
 *            7B: ,vO, @@   iB@:      @B   7@:MB@B@B@@@B@B@BM       @@.    B:  2@q         7@  BS @i 0@  B. @O B@
 *       ,r2EBB@B@B@Bi G@  @BB        B@   @B        @S              :    r@   ..          7B  @F @7  B7 @  @B @B
 *       E@B@UOBr       @B@Bi          L@0PB        .BZ        .@B@B@B@B@B@B@B@B@B@B@B@,   r@  BF @i  @G B@B@B B@
 *            7@,      kB@U    ;r        @@@.       .@Z                GBuL@iBBi           vB@B@q BP:5@7 @u,.  @B
 *            LBi   YB@BrB@    @@       @B:L@Br      BM             .M@B  rB  rB@J         v@. Pi @XZ8r  .     B@
 *        .   G@i B@BM.  ,B@,  @B    iB@B    N, 7r..q@k         ,LB@B8    J@,   i@B@B1r           Br           @@
 *        MB@B@B  ,i       B@B@B,    B@:        @B@B@F         .@BB:      P@i      :OBZ          .@U       B@B@B:
 *                          .ll                                           rB.                     :
 */


#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
using namespace std;
const int maxm=1e5+5;
int a[maxm];
int b[maxm];
int mark[maxm];
int n;
struct Node{
    int in,out;
}e[maxm];
bool cmp(Node a,Node b){
    return a.in<b.in;
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        e[a[i]].in=i;
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
        e[b[i]].out=i;
    }
    sort(e+1,e+1+n,cmp);
    int ans=0;
    int ma=0;
    for(int i=1;i<=n;i++){
        if(ma>e[i].out){
            ans++;
            ma=e[i].out;
        }
    }
    cout<<ans<<endl;
    return 0;
}
/*
 *                             UM.
 *                            J@B@1                                                                    iO@1
 *                           Y@@@B@BB.                                                              7B@B@B@
 *                          :@B@i,B@B@O                                                          ,Z@B@B@B@Br
 *                          @B@q   i@B@BS                                                      7@B@@@O5vMB@q
 *                         8@@B      LB@B@i                                                  FB@@@BNjYjLE@B@
 *                        ,B@B:        0@@@Z                                               P@B@BM1JJ125JPB@B
 *                        B@BB          :@B@B                                            XB@B@Z2LuU52F2u2@B@.
 *                       :@B@             @@@B:                                        v@B@B8uJj51F1525uUB@B7
 *                       @B@O              0@@B.               ..::ir7vvYUuU777r::.   B@B@OULU2F2F151F11Y@B@S
 *                       B@B,               8B@B  :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
 *                      U@B@                 @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
 *                      @B@u                 v@@@@MSur:.                    LB@MvvjJuU5YU252F1F1F25251F2uX@@@
 *                      @@@.                                                N@BML2U2UUU12F15252525251515Jk@@B
 *                     r@B@                                                 YB@Bju52121252515252F15251F2u5@B@
 *                     PB@B                                                  @@@PYUF151F25151F152F2F1F15jF@@B
 *                     @@BS                                                  N@@@UJ2F25252F251525151F1F1u5@B@
 *                     @@@7                                                   B@B@5Yj12F152F1F1F25252515jFB@B
 *                     B@Bi                                                    M@B@O2Luu52525212F151121UY1@B@7
 *                    O@B@:                                                     v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
 *                  7B@B@,                                                        1B@@@B@GPF1uujuu21PNMB@B@B@B@@
 *                 qB@B2                                                            i8B@B@B@B@B@@@@@B@B@B@q: @@@B
 *                MB@B:                                                                 7SBB@B@B@B@B@Zu:      @B@B
 *               ZB@B.                                              ,v.                                        @B@L
 *              LB@B,                         Y7                    @B@Bu                                      7@B@
 *   :B@B@@B2:  @@B7                         @B@Z                   r@B@B@BP:                                   B@BE
 *    BB@@@B@B@B@BE                        r@B@B                       7@B@B@B@Ou:                              iB@B
 *         :uM@@B@@2.           :7::::ivk@B@B@0                           :5B@B@B@B@B@B@G.                       @B@i
 *            BB@@@B@@         :@B@B@@@B@B@@1                                 .i5M@B@B@@@5                       M@@2
 *            B@B ,@B1          L0EZZG0F7:                                            .:,                        uB@MrP@M7
 *           2@B@                                                                                ,O@B@B@B@B
 *           @B@1                                                     :@B@@@r                                :@@@@B@BL:,,
 *           B@Bi                         :2ZS;                      :@B@B@B@r                               L@B@B@BU
 *           @B@.                        @@@B@B@                     vB@B@B@B5                                   @B@i
 *           B@B                        7B@B@B@BM                     OB@B@B@                                   ,B@B
 *           @B@                         @B@B@@@i                       rL7.                                    B@BM
 *           B@B7.:                       NB@@M.                                                               .@B@.
 *  .;JEB@@@B@B@B@B@.                                                                                .  @B@u
 *@@@B@B@B@B@@@B@18U                                                                                :B@B@B@BU,
 *7@BOui.    ,@@B                                                                                SP@B@B@B@B@Or
 *            @@@U                                                                                B@BJ.YO@B@B@i
 *            r@B@                                                                                :B@Bk     .k@B@
 *             B@B@                                                                                LB@@k         2i
 *              B@BM                                      .7jXEGqF7:                                     OB@@L
 *              .B@BM                                   .B@B@B@B@B@B@.                                 :@B@B:
 *               .B@B@                                   @@MYr::ivG@B                                .M@B@G
 *                 B@@@S                                                                           ,MB@B@,
 *                  v@@@BF                                                                      .1B@B@Br
 *                    2@@B@BL                                                                ,FB@@@B8,
 *                      r@B@B@BF,                                                        :YBB@B@B@B
 *                         L@B@B@B@P7,                                           .ivXB@B@B@B@B@M@B@
 *                            ,1B@B@B@B@@@BOP2L7i:,.              ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
 *                              @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
 *                              B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
 *                              @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
 *                              B@@X::,::::iirir;riri:E@B@1         ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
 *                              @@@B@BBq5v7ii:::::::.2@@@i  ..,..     @B@@,,:::::irv2XMB@B@B@B@2@B@:
 *                             .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,,  :.   @B@P50MB@B@B@B@B@@@BS:   @@B1
 *                             E@B@   ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57.        kB@B
 *                            .@B@:          .,ivu5Nq@B@u  ..,.. SB@B@@@B@PL7i,                 ,@B@
 *                            @@@8                   i@B@:    .     :B@B@@                       B@@2
 *                          i@@@@                     0@B@u          B@@B.                       vB@B
 *                         ,@B@G                       L@B@BOv:.:iFB@B@M                          @B@Bi
 *                          vNi                          S@@B@B@B@B@BM:                            MB@N
 *                                                          758BMqJ,
 *
 *                 .  YO.               vq                            :G       Z:
 *        SqOMBB@B@Br @@r rBE           @B     B@@@@@B@ONX8k    i::::.OB1.:::.u@O.::::i           @B@B@U:@@B@@BPEBu
 *        B@@NB@k.    5@i  uB@E.        BM     1U2uUJvirB@@Z   r@@B@B@@@B@B@B@B@B@@@B@Bi   LB@B@1 BX :@k uLLLvr@BJ:
 *            iB      iBi    7@     .@M8@BGMZZ         @@F            ,B       Pi          v@  Bq @i v@        B@
 *       vuL7r8@S7vJL7N@Z7LLri;72.   F7@Bvvv@@       @BX         @@@B@B@@@@@B@@@B@B@B      7@  @F Bi @q  @B@Bu @B
 *       N@B@G@@@8@BBOMB@G@BMNXG@,     B@   @@      .Bk          .:u;    i@:      Zv       7@  Bk @,;@  ,BY @B B@
 *            r@       @G     5.      ,@v   BZ :::,.r@E .::i,      @B     B@    .@BL       7@  @F B:i@. .@  @M @B
 *            7B: ,vO, @@   iB@:      @B   7@:MB@B@B@@@B@B@BM       @@.    B:  2@q         7@  BS @i 0@  B. @O B@
 *       ,r2EBB@B@B@Bi G@  @BB        B@   @B        @S              :    r@   ..          7B  @F @7  B7 @  @B @B
 *       E@B@UOBr       @B@Bi          L@0PB        .BZ        .@B@B@B@B@B@B@B@B@B@B@B@,   r@  BF @i  @G B@B@B B@
 *            7@,      kB@U    ;r        @@@.       .@Z                GBuL@iBBi           vB@B@q BP:5@7 @u,.  @B
 *            LBi   YB@BrB@    @@       @B:L@Br      BM             .M@B  rB  rB@J         v@. Pi @XZ8r  .     B@
 *        .   G@i B@BM.  ,B@,  @B    iB@B    N, 7r..q@k         ,LB@B8    J@,   i@B@B1r           Br           @@
 *        MB@B@B  ,i       B@B@B,    B@:        @B@B@F         .@BB:      P@i      :OBZ          .@U       B@B@B:
 *                          .ll                                           rB.                     :
 */


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vr vector
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
#define loop(i,n) for(i=0;i<n;i++)
ll power(ll x,ll y,ll md)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = ((res%md)*(x%md))%md;

        // y must be even now
        y = y>>1; // y = y/2
        x = ((x%md)*(x%md))%md;  // Change x to x^2
    }
    return res%md;
}

 /*ll arr[2000003];
 ll maxn=2000001;
 ll sieve()
 {
     arr[1]=arr[0]=0;
     for(ll i=2;i<=maxn;i++)
        arr[i]=1;
     for(ll i=2;i*i<=maxn;i++)
     {
         if(arr[i]==1)
         {
             for(ll j=i*i;j<=maxn;j+=i)
                arr[j]=0;
         }
     }

 }*/

int main()
{
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);

   // ll t;
   // cin>>t;
   // while(t--)
    {
      ll n,i,j,c=0,a[200007],b[200007];
      cin>>n;

      loop(i,n)
      cin>>a[i];

      loop(i,n)
      cin>>b[i];

      for(i=0,j=0;i<n;)
      {
          if(a[i]==b[j])
            i++,j++;
          else if(j<n-1)
            j++,c++;
          else
            i++;
      }
      cout<<c;

     cout<<endl;

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vr vector
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
#define loop(i,n) for(i=0;i<n;i++)
ll power(ll x,ll y,ll md)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = ((res%md)*(x%md))%md;

        // y must be even now
        y = y>>1; // y = y/2
        x = ((x%md)*(x%md))%md;  // Change x to x^2
    }
    return res%md;
}

 /*ll arr[2000003];
 ll maxn=2000001;
 ll sieve()
 {
     arr[1]=arr[0]=0;
     for(ll i=2;i<=maxn;i++)
        arr[i]=1;
     for(ll i=2;i*i<=maxn;i++)
     {
         if(arr[i]==1)
         {
             for(ll j=i*i;j<=maxn;j+=i)
                arr[j]=0;
         }
     }

 }*/

int main()
{
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
      ll n,i,j,c=0,a[200007],b[200007];
      cin>>n;

      loop(i,n)
      cin>>a[i];

      loop(i,n)
      cin>>b[i];

      for(i=0,j=0;i<n;)
      {
          if(a[i]==b[j])
            i++,j++;
          else if(j<n-1)
            j++,c++;
          else
            i++;
      }
      cout<<c;

     cout<<endl;

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vr vector
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
#define loop(i,n) for(i=0;i<n;i++)
ll power(ll x,ll y,ll md)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = ((res%md)*(x%md))%md;

        // y must be even now
        y = y>>1; // y = y/2
        x = ((x%md)*(x%md))%md;  // Change x to x^2
    }
    return res%md;
}

 /*ll arr[2000003];
 ll maxn=2000001;
 ll sieve()
 {
     arr[1]=arr[0]=0;
     for(ll i=2;i<=maxn;i++)
        arr[i]=1;
     for(ll i=2;i*i<=maxn;i++)
     {
         if(arr[i]==1)
         {
             for(ll j=i*i;j<=maxn;j+=i)
                arr[j]=0;
         }
     }

 }*/

int main()
{
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);

   // ll t;
    //cin>>t;
    //while(t--)
    {
      ll n,i,j,c=0,a[200007],b[200007];
      cin>>n;

      loop(i,n)
      cin>>a[i];

      loop(i,n)
      cin>>b[i];

      for(i=0,j=0;i<n;)
      {
          if(j==n-1&&a[i]!=b[j])
            i++;
          else if(a[i]==b[j])
            i++,j=min(++j,n-1);
          else
            c++,j++;
      }
      cout<<c;

     cout<<endl;

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vr vector
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
#define loop(i,n) for(i=0;i<n;i++)
ll power(ll x,ll y,ll md)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = ((res%md)*(x%md))%md;

        // y must be even now
        y = y>>1; // y = y/2
        x = ((x%md)*(x%md))%md;  // Change x to x^2
    }
    return res%md;
}

 /*ll arr[2000003];
 ll maxn=2000001;
 ll sieve()
 {
     arr[1]=arr[0]=0;
     for(ll i=2;i<=maxn;i++)
        arr[i]=1;
     for(ll i=2;i*i<=maxn;i++)
     {
         if(arr[i]==1)
         {
             for(ll j=i*i;j<=maxn;j+=i)
                arr[j]=0;
         }
     }

 }*/

int main()
{
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    //ll t;
    //cin>>t;
    //while(t--)
    {
      ll n,i,j,c=0,a[200007],b[200007];
      cin>>n;

      loop(i,n)
      cin>>a[i];

      loop(i,n)
      cin>>b[i];

      for(i=0,j=0;i<n;)
      {
          j=min(j,n-1);
          if(a[i]==b[j])
            i++,j++;
          else if(j<n-1)
            j++,c++;
          else
            i++;
      }
      cout<<c;

     cout<<endl;

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vr vector
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
#define loop(i,n) for(i=0;i<n;i++)
ll power(ll x,ll y,ll md)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = ((res%md)*(x%md))%md;

        // y must be even now
        y = y>>1; // y = y/2
        x = ((x%md)*(x%md))%md;  // Change x to x^2
    }
    return res%md;
}

 /*ll arr[2000003];
 ll maxn=2000001;
 ll sieve()
 {
     arr[1]=arr[0]=0;
     for(ll i=2;i<=maxn;i++)
        arr[i]=1;
     for(ll i=2;i*i<=maxn;i++)
     {
         if(arr[i]==1)
         {
             for(ll j=i*i;j<=maxn;j+=i)
                arr[j]=0;
         }
     }

 }*/

int main()
{
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);

   // ll t;
   // cin>>t;
   // while(t--)
    {
      ll n,i,j,c=0,a[200007],b[200007];
      cin>>n;

      loop(i,n)
      cin>>a[i];

      loop(i,n)
      cin>>b[i];

     ll h[200007]={0};
      for(i=0,j=0;i<n;)
      {
          if(a[i]==b[j])
          i++,j++;
          else
            h[b[j]]=1,j++;

          while(i<n&&h[a[i]]==1)
            i++;

      }
      ll ans=0;
      for(i=1;i<=n;i++)
        ans+=h[i];

      cout<<ans;

     cout<<endl;

    }

    return 0;

}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = n - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (index > out[cur]){
            overTaken += index - out[cur];
            ++fined;
        } else if (index == out[cur]){
            ++fined;
        } else if (overTaken)
            --overTaken;
        order.pop();
        --index;
    }
    cout << fined << endl;
}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = n - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (index > out[cur]){
            overTaken += index - out[cur];
            ++fined;
        } else if (index == out[cur] && overTaken){
            ++fined;
        } else if (overTaken && index < out[cur])
            --overTaken;
        else if (index < out[cur] && !overTaken)
            ++fined;

        order.pop();
        --index;
    }
    cout << fined << endl;
}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, maxi = - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (out[cur] > maxi)
            maxi = out[cur];
        else
            ++fined;
        order.pop();
    }
    cout << fined << endl;
}


#include <iostream>
#include <queue>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    queue <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = 0;

    while (!order.empty()){
        cur = order.front();
        if (out[cur] > index && out[cur] - index > overTaken)
            overTaken += out[cur] - index - overTaken;
        order.pop();
        ++index;
    }
    cout << overTaken << endl;
}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = n - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (index > out[cur]){
            if (overTaken + (index - out[cur]) <= index)
                overTaken += (index - out[cur]);
            else
                overTaken = (index - out[cur]);
            ++fined;
        } else if (index == out[cur] && overTaken)
            ++fined, --overTaken;
        else if (overTaken && index < out[cur])
            --overTaken;

        order.pop();
        --index;
    }
    cout << fined + overTaken << endl;
}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = n - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (index > out[cur]){
            overTaken += index - out[cur];
            ++fined;
        } else if (index == out[cur] && overTaken){
            ++fined;
        } else if (overTaken && index < out[cur])
            --overTaken;
        order.pop();
        --index;
    }
    cout << fined << endl;
}


#include <iostream>
#include <stack>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    stack <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, overTaken = 0, index = n - 1, fined = 0;

    while (!order.empty()){
        cur = order.top();
        if (index > out[cur]){
            overTaken += index - out[cur];
            ++fined;
        } else if (index == out[cur] && overTaken){
            ++fined;
        } else if (overTaken)
            --overTaken;
        order.pop();
        --index;
    }
    cout << fined << endl;
}


#include <iostream>
#include <queue>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;

    queue <int> order;
    map <int, int> out;

    int x;

    for (int i = 0; i < n; ++i)
        cin >> x, order.push(x);

    for (int i = 0; i < n; ++i)
        cin >> x, out[x] = i;

    int cur = 0, maxi = - 1, fined = 0;

    while (!order.empty()){
        cur = order.front();
        if (out[cur] > maxi)
            maxi = out[cur];
        else
            ++fined;
        order.pop();
    }
    cout << fined << endl;
}


#include <bits/stdc++.h>
#include<cmath>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[]= {-1,0,0};
int dy[]= {0,1,-1};
#define endl '\n'
long long mo=1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
long long gcd(long long x,long long y)
{
    if(x==0)
        return y;
    return gcd(y%x,x);
}
long long fast_power(long long x,long long y)
{
    long long p=1;
    while(y)
    {
        if(y%2!=0)
            p=(p*x);
        y/=2;
        x=(x*x);
    }
    return p;
}
long long com(long long n,long long x)
{
    long long p=1,p1=1;
    for(int i=n;i>n-x;i--)
        p=p*i;
    for(int i=1;i<=x;i++)
        p1=p1*i;
    return (p/p1);
}
int main()
{
 trust_your_self();
 //freopen("hussien.in", "r", stdin);
int t,a,b; string s=""; long long c=0;  stringstream ss; char f;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a;
    ss<<a;
    s=ss.str();

}
int l=0;
f=s[0];
//cout<<s<<endl;
for(int i=0;i<t;i++){
        cin>>a;
        stringstream ss1; string c1="";
ss1<<a;
c1=ss1.str();;
if(s.size()>0){
    if(c1[0]!=f){
        c++;
        int id=s.find(c1[0]);
        if(s.find(c1[0])!=-1)
        s.erase(id,1);
    }else{
    int id=s.find(c1[0]);
    if(s.find(c1[0])!=-1)
        s.erase(id,1);
        f=s[0];
    }
}

}
cout<<c;
    return 0;
}


#include <bits/stdc++.h>
#include<cmath>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[]= {-1,0,0};
int dy[]= {0,1,-1};
#define endl '\n'
long long mo=1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
long long gcd(long long x,long long y)
{
    if(x==0)
        return y;
    return gcd(y%x,x);
}
long long fast_power(long long x,long long y)
{
    long long p=1;
    while(y)
    {
        if(y%2!=0)
            p=(p*x);
        y/=2;
        x=(x*x);
    }
    return p;
}
long long com(long long n,long long x)
{
    long long p=1,p1=1;
    for(int i=n;i>n-x;i--)
        p=p*i;
    for(int i=1;i<=x;i++)
        p1=p1*i;
    return (p/p1);
}
int main()
{
 trust_your_self();
 //freopen("hussien.in", "r", stdin);
int t,a[200001],b; string s=""; long long c=0; map<int,int>mp;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a[i];
}
//cout<<s<<endl;
int l=0;
for(int i=0;i<t;i++){
    cin>>b;

if(mp[a[l]]==0&&l<t){
   if(a[l]!=b&&mp[b]==0) {
    c++;
    mp[b]=1;
   }else if(a[l]==b){
       l++;
       mp[b]=1;
   }
}else{
while(mp[a[l]]==1&&l<t){
    l++;
}

}

}
cout<<c;
    return 0;
}


#include <bits/stdc++.h>
#include<cmath>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[]= {-1,0,0};
int dy[]= {0,1,-1};
#define endl '\n'
long long mo=1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
long long gcd(long long x,long long y)
{
    if(x==0)
        return y;
    return gcd(y%x,x);
}
long long fast_power(long long x,long long y)
{
    long long p=1;
    while(y)
    {
        if(y%2!=0)
            p=(p*x);
        y/=2;
        x=(x*x);
    }
    return p;
}
long long com(long long n,long long x)
{
    long long p=1,p1=1;
    for(int i=n;i>n-x;i--)
        p=p*i;
    for(int i=1;i<=x;i++)
        p1=p1*i;
    return (p/p1);
}
int main()
{
 trust_your_self();
 //freopen("hussien.in", "r", stdin);
int t,a,b; string s=""; long long c=0;  stringstream ss;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a;
    ss<<a;
    s=ss.str();

}
int l=0;
//cout<<s<<endl;
for(int i=0;i<t;i++){
        cin>>a;
        stringstream ss1; string c1="";
ss1<<a;
c1=ss1.str();
if(s.size()>0){
    if(c1[0]!=s[0]){
        c++;
        int id=s.find(c1[0]);
        if(s.find(c1[0])!=-1)
        s.erase(id,1);
    }else{
    int id=s.find(c1[0]);
    if(s.find(c1[0])!=-1)
        s.erase(id,1);
    }
}

}
cout<<c;
    return 0;
}


#include <bits/stdc++.h>
#include<cmath>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[]= {-1,0,0};
int dy[]= {0,1,-1};
#define endl '\n'
long long mo=1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
long long gcd(long long x,long long y)
{
    if(x==0)
        return y;
    return gcd(y%x,x);
}
long long fast_power(long long x,long long y)
{
    long long p=1;
    while(y)
    {
        if(y%2!=0)
            p=(p*x);
        y/=2;
        x=(x*x);
    }
    return p;
}
long long com(long long n,long long x)
{
    long long p=1,p1=1;
    for(int i=n;i>n-x;i--)
        p=p*i;
    for(int i=1;i<=x;i++)
        p1=p1*i;
    return (p/p1);
}
int main()
{
 trust_your_self();
 //freopen("hussien.in", "r", stdin);
int t,a,b; map<int,int>mp; long long c=0; vector<int>v;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a;
    v.push_back(a);

}
int l=0;
for(int i=0;i<t;i++){
    cin>>b;
    if(mp[v[l]]==0&&l<v.size()){
    if(b!=v[l]&&mp[b]==0){
        c++;
        mp[b]=1;
    }else
    {
    mp[b]=1;
    l++;
    }
    }
}
cout<<c;
    return 0;
}


#include <iostream>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<deque>
#include<iomanip>
#include<algorithm>
#include<set>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[] = { -1,0,0 };
int dy[] = { 0,1,-1 };
#define endl '\n'
long long mo = 1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
long long gcd(long long x, long long y)
{
	if (x == 0)
		return y;
	return gcd(y%x, x);
}
long long fast_power(long long x, long long y)
{
	long long p = 1;
	while (y)
	{
		if (y % 2 != 0)
			p = (p*x);
		y /= 2;
		x = (x*x);
	}
	return p;
}
long long convert_to_num(string x) {
	long long  num = 0;
	for (int i = 0; i < x.size(); i++) {
		num *= 2;
		num += x[i] - '0';
	}
	return num;
}

long long com(long long n, long long x)
{
	long long p = 1, p1 = 1;
	for (int i = n; i>n - x; i--)
		p = p*i;
	for (int i = 1; i <= x; i++)
		p1 = p1*i;
	return (p / p1);
}

int main()
{
	trust_your_self();
	//freopen("hussien.in", "r", stdin);
	int n, a[100001], b[100001];  map<int, int>mp; long long c = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		mp[a[i]] = i;
	}
	for (int i = 0; i < n; i++)
		cin >> b[i];
	int l = 0;
	for (int i = 0; i < n; ) {
		if (mp[a[l]] != -1) {
			if (b[i] != a[l]) {
				c++;
				mp[b[i]] = -1;
				i++;
			}
			else
			{
				l++;
				i++;
			}
		}
		else
			l++;

	}

	cout << c << endl;

//	system("pause");
	return 0;
}


#include <bits/stdc++.h>
#include<cmath>
using namespace std;
//isdigit x=x*10+(s[i]-'0');
int dx[]= {-1,0,0};
int dy[]= {0,1,-1};
#define endl '\n'
long long mo=1e9;
const long double P = 3.141592653589793238L;
void trust_your_self()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
long long gcd(long long x,long long y)
{
    if(x==0)
        return y;
    return gcd(y%x,x);
}
long long fast_power(long long x,long long y)
{
    long long p=1;
    while(y)
    {
        if(y%2!=0)
            p=(p*x);
        y/=2;
        x=(x*x);
    }
    return p;
}
long long com(long long n,long long x)
{
    long long p=1,p1=1;
    for(int i=n;i>n-x;i--)
        p=p*i;
    for(int i=1;i<=x;i++)
        p1=p1*i;
    return (p/p1);
}
int main()
{
 trust_your_self();
 //freopen("hussien.in", "r", stdin);
int t,a,b; map<int,int>mp; long long c=0; vector<int>v;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a;
    v.push_back(a);

}
int l=0;
for(int i=0;i<t;i++){
    cin>>b;
    if(mp[v[l]]==0&&l<v.size()){
    if(b!=v[l]&&mp[b]==0){
        c++;
        mp[b]=1;
    }else if(b==v[l])
    {
    mp[b]=1;
    l++;
    }
    }
}
cout<<c;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];

    for(int i=0;i<n;i++){
        cin>>a[i];

    }
    for(int i=0;i<n;i++){
        cin>>b[i];

    }
    int s1=0,s2=0;
    int ma = INT_MIN;

    while(s1<n && s2<n){
        int count =0;

        while(a[s1]!=b[s2] && (s1<n && s2<n)){

            s2++;
            count++;

        }
        s1++;
        s2++;
        ma = max(count,ma);
    }
    cout<<ma<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];
    map <int,int> a1,a2;

    for(int i=0;i<n;i++){
        cin>>a[i];
        a1[a[i]] = i;
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
        a2[b[i]]=i;
    }
    int count =0;
    for(int i=0;i<n;i++){
        if(a1[a[i]]>=a2[a[i]]){
            count++;
        }
    }
    if(count==n){
        cout<<0<<endl;
    }
    else{
    cout<<count<<endl;
    }

}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];
    map <int,int> a1,a2;

    for(int i=0;i<n;i++){
        cin>>a[i];
        a1[a[i]] = i;
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
        a2[b[i]]=i;
    }
    int count =0;
    set <int> st;
    for(int i=0;i<n;i++){
        map <int,int> mp;
        for(int j=0;j<i;j++){
            mp[a[j]]++;
        }
        for(int j=0;j<a2[a[i]];j++){
            if(mp[b[j]]-1!=0){
                st.insert(b[j]);
            }
        }
    }
    cout<<st.size()<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];

    for(int i=0;i<n;i++){
        cin>>a[i];

    }
    int len =0;
    for(int i=0;i<n;i++){
        cin>>b[i];
        if(a[i]==b[i]){
            len++;
        }

    }
    if(len==n){
        cout<<0<<endl;

    }
    else{

    int s1=0,s2=0;
    int ma = INT_MIN;

    while(s1<n && s2<n){
        int count =0;

        while( (s1<n && s2<n) && a[s1]!=b[s2]){

            s2++;
            count++;

        }
        s1++;

        ma = max(count,ma);
    }
    cout<<ma<<endl;

    }

}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];

    int c[n]; map<int,int> mp;
    for(int i=0;i<n;i++){
        cin>>a[i];

    }
    for(int i=0;i<n;i++){
        cin>>b[i];
        mp[b[i]] = i;
    }
    for(int i=0;i<n;i++){
        c[i] = mp[a[i]];
    }
    int ma = INT_MIN; int count =0;
    for(int i=0;i<n;i++){
        if(ma>c[i]){
            count++;
        }

        ma = max(ma,c[i]);
    }
    cout<<count<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
    int n; cin>>n;
    int a[n],b[n];

    for(int i=0;i<n;i++){
        cin>>a[i];

    }
    for(int i=0;i<n;i++){
        cin>>b[i];

    }
    int s1=0,s2=0;
    int ma = INT_MIN;

    while(s1<n && s2<n){
        int count =0;

        if(a[s1]!=b[s2]){
        while(a[s1]!=b[s2] && (s1<n && s2<n)){

            s2++;
            count++;

        }
        s1++;
        }
        else{
            s1++;
            s2++;
        }
        ma = max(count,ma);
    }
    cout<<ma<<endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int in[100006];
vector <int>out;
int outp[100006];

int main() {
    set<pair<int,bool>> fined;
    int n;
    cin>>n;
    for(int i=n-1;i>=0;i--){
        cin>>in[i];
    }
    for(int i=n-1;i>=0;i--){
        int curr;
        cin>>curr;
        out.push_back(curr);
        outp[curr]=i;
    }
    reverse(out.begin(),out.end());
    for(int i=n-1;i>=0;i--){
        int x=outp[in[i]];
        if(fined.find(make_pair(out[x],false))==fined.end()&&fined.find(make_pair(out[x],true))==fined.end()){
            fined.insert(make_pair(out[x],false));
            out.erase(out.begin()+x);
        }
        for(int j=x;j<n;j++){
            if(fined.find(make_pair(out[j],false))==fined.end()&&fined.find(make_pair(out[j],true))==fined.end()){
                fined.insert(make_pair(out[j],true));
            }
        }
    }
    int res=0;
    for(pair<int,bool> x:fined){
        if(x.second==true){
            res++;
        }
    }
    cout<<res;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int in[100006],out[100006];
int outp[100006];

int main() {
    set<pair<int,bool>> fined;
    int n;
    cin>>n;
    for(int i=n-1;i>=0;i--){
        cin>>in[i];
    }
    for(int i=n-1;i>=0;i--){
        int curr;
        cin>>curr;
        out[i]=curr;
        outp[curr]=i;
    }
    for(int i=n-1;i>=0;i--){
        int x=outp[in[i]];
        if(fined.find(make_pair(out[x],false))==fined.end()&&fined.find(make_pair(out[x],true))==fined.end()){
            fined.insert(make_pair(out[x],false));
        }
        for(int j=x+1;j<n;j++){
            if(fined.find(make_pair(out[j],false))==fined.end()&&fined.find(make_pair(out[j],true))==fined.end()){
                fined.insert(make_pair(out[j],true));
            }
        }
    }
    int res=0;
    for(pair<int,bool> x:fined){
        if(x.second==true){
            res++;
        }
    }
    cout<<res;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int in[100006],out[100006];
int locate(int x,int size){
    for(int i=0;i<size;i++){
        if(x==out[i]){
            return i;
        }
    }
}
int main() {
    set<pair<int,bool>> fined;
    int n;
    cin>>n;
    for(int i=n-1;i>=0;i--){
        cin>>in[i];
    }
    for(int i=n-1;i>=0;i--){
        cin>>out[i];
    }
    for(int i=n-1;i>=0;i--){
        int x=locate(in[i],n);
        if(fined.find(make_pair(out[x],false))==fined.end()&&fined.find(make_pair(out[x],true))==fined.end()){
            fined.insert(make_pair(out[x],false));
        }
        for(int j=x+1;j<n;j++){
            if(fined.find(make_pair(out[j],false))==fined.end()&&fined.find(make_pair(out[j],true))==fined.end()){
                fined.insert(make_pair(out[j],true));
            }
        }
    }
    int res=0;
    for(pair<int,bool> x:fined){
        if(x.second==true){
            res++;
        }
    }
    cout<<res;
    return 0;
}


    #include<bits/stdc++.h>
    using namespace std;
    int enter[100007];
    int exits[100007];
    bool finder[100007];
    int main(){
    	int n;
    	cin>>n;
    	for(int i=0;i<n;i++){
    		scanf("%d",&enter[i]);
    	}
    	for(int i=0;i<n;i++){
    		scanf("%d",&exits[i]);
    	}
    	int i=n-1,j=n-1;
    	int cnt=0;
    	while(i>=0){
    		if(finder[exits[j]]==true||enter[i]==exits[j]){
    			if(finder[exits[j]]==true){
    				j--;
    			}
    			else{
    				i--;
    				j--;
    			}
    			continue;
    		}
    		finder[enter[i]]=true;
    		cnt++;
    		i--;
    	}
    	cout<<cnt<<endl;
    }


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int in[100006];
int out[100006];
int outp[100006];

int main() {
    set<pair<int,bool>> fined;
    int n;
    cin>>n;
    for(int i=n-1;i>=0;i--){
        cin>>in[i];
    }
    for(int i=n-1;i>=0;i--){
        int curr;
        cin>>curr;
        out[i]=curr;
        outp[curr]=i;
    }
    for(int i=n-1;i>(n+1)/2;i--){
        int x=outp[in[i]];
        if(fined.find(make_pair(out[x],false))==fined.end()&&fined.find(make_pair(out[x],true))==fined.end()){
            fined.insert(make_pair(out[x],false));
        }
        for(int j=x+1;j<n;j++){
            if(fined.find(make_pair(out[j],false))==fined.end()&&fined.find(make_pair(out[j],true))==fined.end()){
                fined.insert(make_pair(out[j],true));
            }
        }
    }
    for(int i=(n+1)/2;i>=0;i--){
        int x=outp[in[i]];
        if(fined.find(make_pair(out[x],false))==fined.end()&&fined.find(make_pair(out[x],true))==fined.end()){
            fined.insert(make_pair(out[x],false));
        }
        for(int j=x+1;j<n;j++){
            if(fined.find(make_pair(out[j],false))==fined.end()&&fined.find(make_pair(out[j],true))==fined.end()){
                fined.insert(make_pair(out[j],true));
            }
        }
    }
    int res=0;
    for(pair<int,bool> x:fined){
        if(x.second==true){
            res++;
        }
    }
    cout<<res;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
int q[2000001]={0};
int main()
{int i,n,m,a,b,c;
      // freopen ("input.txt","r",stdin);
     // freopen ("output.txt","w",stdout);
     cin>>n>>m;
     for(i=0;i<m;i++){
                      cin>>a>>b>>c;
     if(q[a]==0 && q[b]==0 && q[c]==0){
                q[a]=1;q[b]=2;q[c]=3;}
     else {
          if(q[a]==1){q[b]=2;q[c]=3;continue;}
          if(q[a]==2){q[b]=1;q[c]=3;continue;}
          if(q[a]==3){q[b]=1;q[c]=2;continue;}
          if(q[b]==1){q[a]=2;q[c]=3;continue;}
          if(q[b]==2){q[a]=1;q[c]=3;continue;}
          if(q[b]==3){q[a]=1;q[a]=2;continue;}
          if(q[c]==1){q[b]=2;q[a]=3;continue;}
          if(q[c]==2){q[b]=1;q[a]=3;continue;}
          if(q[c]==3){q[b]=1;q[a]=2;continue;}
          }
          }
          for(i=1;i<=n;i++)cout<<q[i]<<" ";
    //system("pause");
    return 0;
      }


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
int q[1000001]={0};
int main()
{int i,n,m,a,b,c;
      // freopen ("input.txt","r",stdin);
     // freopen ("output.txt","w",stdout);
     cin>>n>>m;
     for(i=0;i<m;i++){
                      cin>>a>>b>>c;
     if(q[a]==0 && q[b]==0 && q[c]==0){
                q[a]=1;q[b]=2;q[c]=3;continue;}
          if(q[a]==1){q[b]=2;q[c]=3;continue;}
          if(q[a]==2){q[b]=1;q[c]=3;continue;}
          if(q[a]==3){q[b]=1;q[c]=2;continue;}
          if(q[b]==1){q[a]=2;q[c]=3;continue;}
          if(q[b]==2){q[a]=1;q[c]=3;continue;}
          if(q[b]==3){q[a]=1;q[c]=2;continue;}
          if(q[c]==1){q[b]=2;q[a]=3;continue;}
          if(q[c]==2){q[b]=1;q[a]=3;continue;}
          if(q[c]==3){q[b]=1;q[a]=2;continue;}
          }
          for(i=1;i<=n;i++)cout<<q[i]<<" ";
    //system("pause");
    return 0;
      }


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
int n,m;
struct par
{
    int id,cnt;
}pa[100005];
vector <int> take[100005];
int ans[100005];
bool can[100005][3];
bool cmp(par a,par b)
{
    return a.cnt>b.cnt;
}

int main()
{
    scanf("%d%d",&n,&m);
    int i,j;
    for(i=1;i<=n;++i)
    {
        pa[i].id=i;
        pa[i].cnt=0;
        take[i].clear();
    }
    for(i=0;i<m;++i)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        pa[a].cnt++;
        take[a].push_back(b);
        take[a].push_back(c);
        pa[b].cnt++;
        take[b].push_back(a);
        take[b].push_back(c);
        pa[c].cnt++;
        take[c].push_back(a);
        take[c].push_back(b);
    }
    sort(pa+1,pa+n+1,cmp);
    memset(can,0,sizeof(can));
    memset(ans,0,sizeof(ans));
    int k;
    for(i=1;i<=n;++i)
    {
        for(j=0;j<3;++j) if(!can[pa[i].id][j])
        {
            ans[pa[i].id]=j+1;
            for(k=0;k<take[pa[i].id].size();++k) can[take[pa[i].id][k]][j]=1;
            break;
        }
    }
    for(i=1;i<=n;++i)
    {
        if(!ans[i]) while(1);
        printf("%d ",ans[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
int n,m;
struct par
{
    int id;
}pa[100005];
set <int> take[100005];
set <int>::iterator it;
int ans[100005];
bool can[100005][3];
bool cmp(par a,par b)
{
    return take[a.id].size()>take[b.id].size();
}

int main()
{
    scanf("%d%d",&n,&m);
    int i,j;
    for(i=1;i<=n;++i) pa[i].id=i;
    for(i=0;i<m;++i)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        take[a].insert(b);
        take[a].insert(c);
        take[b].insert(a);
        take[b].insert(c);
        take[c].insert(a);
        take[c].insert(b);
    }
    sort(pa+1,pa+n+1,cmp);
    memset(can,0,sizeof(can));
    for(i=1;i<=n;++i)
    {
        for(j=0;j<3;++j) if(!can[pa[i].id][j])
        {
            ans[pa[i].id]=j+1;
            for(it=take[pa[i].id].begin();it!=take[pa[i].id].end();it++) can[*it][j]=1;
            break;
        }
    }
    for(i=1;i<=n;++i)
    {
        if(!ans[i]) while(1);
        printf("%d ",ans[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int col[maxn];

int main()
{
    int i,n,m;
    scanf("%d%d",&n,&m);
    memset(col,-1,sizeof(col));
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        if(col[a]!=-1)
        {
            col[b]=(col[a]+1)%3;
            col[c]=(col[a]+2)%3;
        }
        else if(col[b]!=-1)
        {
            col[a]=(col[b]+1)%3;
            col[c]=(col[b]+2)%3;
        }
        else if(col[c]!=-1)
        {
            col[a]=(col[c]+1)%3;
            col[b]=(col[c]+2)%3;
        }
        else
        {
            col[a]=0;
            col[b]=1;
            col[c]=2;
        }
    }
    for(i=1;i<=n;++i) printf("%d ",col[i]+1);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
int n,m;
struct par
{
    int id,cnt;
}pa[100005];
vector <int> take[100005];
int ans[100005];
bool can[100005][3];
bool cmp(par a,par b)
{
    return a.cnt>b.cnt;
}

int main()
{
    scanf("%d%d",&n,&m);
    int i,j;
    for(i=1;i<=n;++i) pa[i].id=i;
    for(i=0;i<m;++i)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        pa[a].cnt++;
        take[a].push_back(b);
        take[a].push_back(c);
        pa[b].cnt++;
        take[b].push_back(a);
        take[b].push_back(c);
        pa[c].cnt++;
        take[c].push_back(a);
        take[c].push_back(b);
    }
    sort(pa+1,pa+n+1,cmp);
    memset(can,0,sizeof(can));
    int k;
    for(i=1;i<=n;++i)
    {
        for(j=0;j<3;++j) if(!can[pa[i].id][j])
        {
            ans[pa[i].id]=j+1;
            for(k=0;k<take[pa[i].id].size();++k) can[take[pa[i].id][k]][j]=1;
            break;
        }
    }
    for(i=1;i<=n;++i) printf("%d ",ans[i]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int col[maxn];

int main()
{
    int i,n,m;
    scanf("%d%d",&n,&m);
    memset(col,-1,sizeof(col));
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        if(col[a]!=-1)
        {
            col[b]=(col[a]+1)%3;
            col[c]=(col[a]+2)%3;
        }
        else if(col[b]!=-1)
        {
            col[a]=(col[b]+1)%3;
            col[c]=(col[b]+2)%3;
        }
        else if(col[c]!=-1)
        {
            col[a]=(col[c]+1)%3;
            col[b]=(col[c]+1)%3;
        }
        else
        {
            col[a]=0;
            col[b]=1;
            col[c]=2;
        }
    }
    for(i=1;i<=n;++i) printf("%d ",col[i]+1);
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int ans[111111] = {0};

int main()
{
    int n;
    int m;
    cin >> n;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        int x,y,z;
        cin >> x; cin >> y; cin >> z;
        if(!ans[x]&&!ans[y]&&!ans[z])
        {
            ans[x] = 1;
            ans[y] = 2;
            ans[z] = 3;
        }
        else if(ans[x])
        {
            ans[y] = (ans[x]+1)%3;
            ans[z] = (ans[x]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[y])
        {
            ans[x] = (ans[y]+1)%3;
            ans[z] = (ans[y]+2)%3;
            if(ans[x] == 0)
                ans[x] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[z])
        {
            ans[y] = (ans[z]+1)%3;
            ans[x] = (ans[z]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[x] == 0)
                ans[x] = 3;
        }
    }
    for(int i = 1; i <= n;i++)
    {
        if(ans[i])
            cout << ans[i] << " ";
        else
            cout << 1 << " ";
    }
    cout << endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int ans[111111] = {0};

int main()
{
    int n;
    int m;
    cin >> n;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        int x,y,z;
        cin >> x; cin >> y; cin >> z;
        if(!ans[x]&&!ans[y]&&!ans[z])
        {
            ans[x] = 1;
            ans[y] = 2;
            ans[z] = 3;
            cout << ans[y] << endl;
        }
        else if(ans[x])
        {
            ans[y] = (ans[x]+1)%3;
            ans[z] = (ans[x]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[y])
        {
            ans[x] = (ans[y]+1)%3;
            ans[z] = (ans[y]+2)%3;
            if(ans[x] == 0)
                ans[x] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[z])
        {
            ans[y] = (ans[z]+1)%3;
            ans[x] = (ans[z]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[x] == 0)
                ans[x] = 3;
        }
    }
    for(int i = 1; i <= n;i++)
    {
        if(ans[i])
            cout << ans[i] << " ";
        else
            cout << 1 << " ";
    }
    cout << endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int ans[111111] = {0};

int main()
{
    int n;
    int m;
    cin >> n;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        int x,y,z;
        cin >> x; cin >> y; cin >> z;
        if(!ans[x]&&!ans[y]&&!ans[z])
        {
            ans[x] = 1;
            ans[y] = 2;
            ans[z] = 3;
        }
        else if(ans[x])
        {
            ans[y] = (ans[x]+1)%3;
            ans[z] = (ans[x]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[y])
        {
            ans[x] = (ans[y]+1)%3;
            ans[z] = (ans[y]+2)%3;
            if(ans[x] == 0)
                ans[y] = 3;
            if(ans[z] == 0)
                ans[z] = 3;
        }
        else if(ans[z])
        {
            ans[y] = (ans[z]+1)%3;
            ans[x] = (ans[z]+2)%3;
            if(ans[y] == 0)
                ans[y] = 3;
            if(ans[x] == 0)
                ans[z] = 3;
        }
    }
    for(int i = 1; i <= n;i++)
    {
        if(ans[i])
            cout << ans[i] << " ";
        else
            cout << 1 << " ";
    }
    cout << endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int ans[111111] = {0};

int main()
{
    int n;
    int m;
    cin >> n;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        int x,y,z;
        cin >> x; cin >> y; cin >> z;
        if(!ans[x]&&!ans[y]&&!ans[z])
        {
            ans[x] = 1;
            ans[y] = 2;
            ans[z] = 3;
        }
        else if(ans[x])
        {
            ans[y] = (ans[x]+1)%3;
            ans[z] = (ans[x]+2)%3;
        }
        else if(ans[y])
        {
            ans[x] = (ans[y]+1)%3;
            ans[z] = (ans[y]+2)%3;
        }
        else if(ans[z])
        {
            ans[y] = (ans[z]+1)%3;
            ans[x] = (ans[z]+2)%3;
        }
    }
    for(int i = 1; i <= n;i++)
    {
        if(ans[i])
            cout << ans[i] << " ";
        else
            cout << 1 << " ";
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repn(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define max2(x, y) ((x) > (y)) ? (x) : (y)
#define max3(x,y,z) max2((x), max2((y),(z)))
#define max4(v,w,x,y) max2(max2((v),(w)), max2((x),(y)))
#define min2(x, y) ((x) > (y)) ? (y) : (x)
#define min3(x,y,z) min2((x), min2((y),(z)))
#define min4(v,w,x,y) min2(min2((v),(w)), min2((x),(y)))
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int n, m;
int ans[100001];
int main(void){
    cin >> n >> m;
    while(m-- > 0){
        int a[3];
        set<int> st;
        st.insert(1);
        st.insert(2);
        st.insert(3);
        rep(i,3)
            cin >> a[i];
        rep(i, 3){
            if(0 < ans[a[i]]){
                cout << "remove " << *(st.find(ans[a[i]])) << endl;
                st.erase(st.find(ans[a[i]]));
            }
            else{
                ans[a[i]] = *(st.begin());
                st.erase(st.begin());
            }
        }
    }
    repn(i, n - 1) cout << ans[i] << " ";
    cout << ans[n] << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repn(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define max2(x, y) ((x) > (y)) ? (x) : (y)
#define max3(x,y,z) max2((x), max2((y),(z)))
#define max4(v,w,x,y) max2(max2((v),(w)), max2((x),(y)))
#define min2(x, y) ((x) > (y)) ? (y) : (x)
#define min3(x,y,z) min2((x), min2((y),(z)))
#define min4(v,w,x,y) min2(min2((v),(w)), min2((x),(y)))
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int n, m;
int ans[100001];
int main(void){
    cin >> n >> m;
    while(m-- > 0){
        int a[3];
        set<int> st;
        st.insert(1);
        st.insert(2);
        st.insert(3);
        rep(i,3)
            cin >> a[i];
        rep(i, 3){
            if(0 < ans[a[i]])
                st.erase(st.find(ans[a[i]]));
        }
        rep(i, 3){
            if(ans[a[i]] == 0){
                ans[a[i]] = *(st.begin());
                st.erase(st.begin());
            }
        }
    }
    repn(i, n - 1) cout << ans[i] << " ";
    cout << ans[n] << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repn(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define max2(x, y) ((x) > (y)) ? (x) : (y)
#define max3(x,y,z) max2((x), max2((y),(z)))
#define max4(v,w,x,y) max2(max2((v),(w)), max2((x),(y)))
#define min2(x, y) ((x) > (y)) ? (y) : (x)
#define min3(x,y,z) min2((x), min2((y),(z)))
#define min4(v,w,x,y) min2(min2((v),(w)), min2((x),(y)))
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int n, m;
int ans[100001];
int main(void){
    cin >> n >> m;
    while(m-- > 0){
        int a[3];
        set<int> st;
        st.insert(1);
        st.insert(2);
        st.insert(3);
        rep(i,3)
            cin >> a[i];
        rep(i, 3){
            if(0 < ans[a[i]]){
                st.erase(st.find(ans[a[i]]));
            }
            else{
                ans[a[i]] = *(st.begin());
                st.erase(st.begin());
            }
        }
    }
    repn(i, n - 1) cout << ans[i] << " ";
    cout << ans[n] << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repn(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define max2(x, y) ((x) > (y)) ? (x) : (y)
#define max3(x,y,z) max2((x), max2((y),(z)))
#define max4(v,w,x,y) max2(max2((v),(w)), max2((x),(y)))
#define min2(x, y) ((x) > (y)) ? (y) : (x)
#define min3(x,y,z) min2((x), min2((y),(z)))
#define min4(v,w,x,y) min2(min2((v),(w)), min2((x),(y)))
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
    return (st.find(val) != st.end());
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int n, m;
int ans[100001];
int oc[100001];
int main(void){
    cin >> n >> m;
    while(m-- > 0){
        int a[3];
        rep(i,3){
            cin >> a[i];
            oc[a[i]]++;
        }
        rep(i, 3){
            if(2 <= oc[a[i]])
                swap(a[i], a[ans[a[i]]]);
        }
        rep(i, 3)
            ans[a[i]] = i + 1;
    }
    repn(i, n - 1) cout << ans[i] << " ";
    cout << ans[n] << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repn(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define max2(x, y) ((x) > (y)) ? (x) : (y)
#define max3(x,y,z) max2((x), max2((y),(z)))
#define max4(v,w,x,y) max2(max2((v),(w)), max2((x),(y)))
#define min2(x, y) ((x) > (y)) ? (y) : (x)
#define min3(x,y,z) min2((x), min2((y),(z)))
#define min4(v,w,x,y) min2(min2((v),(w)), min2((x),(y)))
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int n, m;
int ans[100001];
int oc[100001];
int main(void){
    cin >> n >> m;
    while(m-- > 0){
        int a[3];
        rep(i,3){
            cin >> a[i];
            oc[a[i]]++;
        }
        rep(i, 3){
            if(2 <= oc[a[i]])
                swap(a[i], a[ans[a[i]]]);
        }
        rep(i, 3)
            ans[a[i]] = i + 1;
    }
    repn(i, n - 1) cout << ans[i] << " ";
    cout << ans[n - 1] << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>

#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a))
#define MP(a,b) make_pair((a),(b))

template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;

const int INF = 0x3fffffff;
const LL LINF = 1LL << 60;
const double PI = acos(-1.0);
const double EPS = 1e-9;

class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};

int col[100010];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(col, 0, sizeof(col));
	for (int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		set<int> S;
		S.insert(1);
		S.insert(2);
		S.insert(3);
		if (!col[a]) col[a] = *S.begin();
		S.erase(col[a]);
		if (!col[b]) col[b] = *S.begin();
		S.erase(col[b]);
		if (!col[c]) col[c] = *S.begin();
		S.erase(col[c]);
	}
	for (int i = 1; i <= n; i++){
		printf("%d%c", col[i] ? col[i] : 1, i == n ? '\n' : ' ');
	}
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>

#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a))
#define MP(a,b) make_pair((a),(b))

template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;

const int INF = 0x3fffffff;
const LL LINF = 1LL << 60;
const double PI = acos(-1.0);
const double EPS = 1e-9;

class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};

int col[100010];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(col, 0, sizeof(col));
	for (int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		set<int> S;
		S.insert(1);
		S.insert(2);
		S.insert(3);
		S.erase(col[a]);
		S.erase(col[b]);
		S.erase(col[c]);
		if (!col[a]) col[a] = *S.begin();
		S.erase(col[a]);
		if (!col[b]) col[b] = *S.begin();
		S.erase(col[b]);
		if (!col[c]) col[c] = *S.begin();
		S.erase(col[c]);
	}
	for (int i = 1; i <= n; i++){
		printf("%d%c", col[i] ? col[i] : 1, i == n ? '\n' : ' ');
	}
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>

#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a))
#define MP(a,b) make_pair((a),(b))

template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;

const int INF = 0x3fffffff;
const LL LINF = 1LL << 60;
const double PI = acos(-1.0);
const double EPS = 1e-9;

class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};

int col[100010];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(col, 0, sizeof(col));
	for (int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		set<int> S;
		S.insert(1);
		S.insert(2);
		S.insert(3);
		S.erase(col[a]);
		S.erase(col[b]);
		S.erase(col[c]);
		if (!col[a]) col[a] = *S.begin();
		if (!col[b]) col[b] = *S.begin();
		if (!col[c]) col[c] = *S.begin();
	}
	for (int i = 1; i <= n; i++){
		printf("%d%c", col[i] ? col[i] : 1, i == n ? '\n' : ' ');
	}
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>

#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a))
#define MP(a,b) make_pair((a),(b))

template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;

const int INF = 0x3fffffff;
const LL LINF = 1LL << 60;
const double PI = acos(-1.0);
const double EPS = 1e-9;

class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};

int col[100010];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(col, 0, sizeof(col));
	for (int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		set<int> S;
		S.insert(1);
		S.insert(2);
		S.insert(3);
		if (!col[a]) col[a] = *S.begin();
		S.erase(a);
		if (!col[b]) col[b] = *S.begin();
		S.erase(b);
		if (!col[c]) col[c] = *S.begin();
		S.erase(c);
	}
	for (int i = 1; i <= n; i++){
		printf("%d%c", col[i] ? col[i] : 1, i == n ? '\n' : ' ');
	}
    return 0;
}


#include <iostream>

using namespace std;

int main() {
    int n, m;
    cin>>n>>m;
    //int **arr = new int*[m];
    int arr[3];
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 3; j++) {
            cin>>arr[j];
        }
        int colors[3];
    for (int i = 0; i < 3; i++) {
        colors[i] = 0;
    }
    for (int j = 0; j < 3; j++) {
        if (people[arr[j] - 1] != 0) {
            int temp = people[arr[j] - 1];
            colors[temp - 1] = 1;
        }
    }
    int k = 0;
    for (int j = 0; j < 3; j++) {
        if (colors[j] != 0) {
            continue;
        }
        for (; k < 3; k++) {
            if (people[arr[k] - 1] == 0) {
                people[arr[k] - 1] = j + 1;
                //k++;
                break;
            }
        }
    }
    }

    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }

    return 0;
}


#include <iostream>

using namespace std;

int paint(int **arr, int *people, int n, int m) {
 int *colors = new int[3];
    for (int i = 0; i < m; i++) {

        for (int j = 0; j < 3; j++) {
            colors[j] = 0;
        }
        for (int j = 0; j < 3; j++) {
            if (people[arr[i][j] - 1] != 0) {
                int temp = people[arr[i][j] - 1];
                colors[temp - 1] = 1;
            }
        }
        int k = 0;
        for (int j = 0; j < 3; j++) {
            if (colors[j] != 0) {
                continue;
            }
            for (; k < 3; k++) {
                if (people[arr[i][k] - 1] == 0) {
                    people[arr[i][k] - 1] = j + 1;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }
    //return 0;
}

int main() {
    int n, m;
    cin>>n>>m;
    int **arr = new int*[m];
    for (int i = 0; i < m; i++) {
        arr[i] = new int[3];
        for (int j = 0; j < 3; j++) {
            cin>>arr[i][j];
        }
    }
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    paint(arr, people, n, m);

    return 0;
}


#include <iostream>

using namespace std;

int paint(int **arr, int *people, int n, int m) {
    for (int i = 0; i < m; i++) {
        int *colors = new int[3];
        for (int j = 0; j < 3; j++) {
            colors[j] = 0;
        }
        for (int j = 0; j < 3; j++) {
            if (people[arr[i][j] - 1] != 0) {
                int temp = people[arr[i][j] - 1];
                colors[temp - 1] = 1;
            }
        }
        int k = 0;
        for (int j = 0; j < 3; j++) {
            if (colors[j] != 0) {
                continue;
            }
            for (; k < 3; k++) {
                if (people[arr[i][k] - 1] == 0) {
                    people[arr[i][k] - 1] = j + 1;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }
    //return 0;
}

int main() {
    int n, m;
    cin>>n>>m;
    int **arr = new int*[m];
    for (int i = 0; i < m; i++) {
        arr[i] = new int[3];
        for (int j = 0; j < 3; j++) {
            cin>>arr[i][j];
        }
    }
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    paint(arr, people, n, m);

    return 0;
}


#include <iostream>
using namespace std;

int paint(int *arr, int *people, int n, int m) {
    int colors[3];
    for (int i = 0; i < 3; i++) {
        colors[i] = 0;
    }
    for (int j = 0; j < 3; j++) {
        if (people[arr[j] - 1] != 0) {
            int temp = people[arr[j] - 1];
            colors[temp - 1] = 1;
        }
    }
    int k = 0;
    for (int j = 0; j < 3; j++) {
        if (colors[j] != 0) {
            continue;
        }
        for (; k < 3; k++) {
            if (people[arr[k] - 1] == 0) {
                people[arr[k] - 1] = j + 1;
                //k++;
                break;
            }
        }
    }
    return 0;
}

int main() {
    int n, m;
    cin>>n>>m;
    //int **arr = new int*[m];
    int arr[3];
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 3; j++) {
            cin>>arr[j];
        }
        paint(arr, people, n, m);
    }

    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }

    return 0;
}


#include <iostream>
using namespace std;

int paint(int **arr, int *people, int n, int m) {
    int *colors = new int[3];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 3; j++) {
            colors[j] = 0;
        }
        for (int j = 0; j < 3; j++) {
            if (people[arr[i][j] - 1] != 0) {
                int temp = people[arr[i][j] - 1];
                colors[temp - 1] = 1;
            }
        }
        int k = 0;
        for (int j = 0; j < 3; j++) {
            if (colors[j] != 0) {
                continue;
            }
            for (; k < 3; k++) {
                if (people[arr[i][k] - 1] == 0) {
                    people[arr[i][k] - 1] = j + 1;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }
    delete []colors;
    return 0;
}

int main() {
    int n, m;
    cin>>n>>m;
    int **arr = new int*[m];
    for (int i = 0; i < m; i++) {
        arr[i] = new int[3];
        for (int j = 0; j < 3; j++) {
            cin>>arr[i][j];
        }
    }
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    paint(arr, people, n, m);
    delete []people;
    delete []arr;
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    int n, m;
    cin>>n>>m;
    //int **arr = new int*[m];
    int arr[3];
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 3; j++) {
            cin>>arr[j];
        }
        int colors[3];
    for (int i = 0; i < 3; i++) {
        colors[i] = 0;
    }
    for (int j = 0; j < 3; j++) {
        if (people[arr[j] - 1] != 0) {
            int temp = people[arr[j] - 1];
            colors[temp - 1] = 1;
        }
    }
    int k = 0;
    for (int j = 0; j < 3; j++) {
        if (colors[j] != 0) {
            continue;
        }
        for (; k < 3; k++) {
            if (people[arr[k] - 1] == 0) {
                people[arr[k] - 1] = j + 1;
                //k++;
                break;
            }
        }
    }
    }

    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }

    return 0;
}


#include <iostream>

using namespace std;

int main() {
    int n, m;
    cin>>n>>m;
    //int **arr = new int*[m];
    int arr[3];
    int colors[3];
    int count = 0;
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < 3; j++) {
            cin>>arr[j];
        }
        if (count < n) {
            for (int i = 0; i < 3; i++) {
                colors[i] = 0;
            }
            for (int j = 0; j < 3; j++) {
                if (people[arr[j] - 1] != 0) {
                    int temp = people[arr[j] - 1];
                    colors[temp - 1] = 1;
                }
            }
            int k = 0;
            for (int j = 0; j < 3; j++) {
                if (colors[j] != 0) {
                    continue;
                }
                for (; k < 3; k++) {
                    if (people[arr[k] - 1] == 0) {
                        people[arr[k] - 1] = j + 1;
                        count++;
                        break;
                    }
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }

    return 0;
}


#include <iostream>

using namespace std;

int paint(int **arr, int *people, int n, int m) {
    for (int i = 0; i < m; i++) {
        int *colors = new int[3];
        for (int j = 0; j < 3; j++) {
            colors[j] = 0;
        }
        for (int j = 0; j < 3; j++) {
            if (people[arr[i][j]] != 0) {
                int temp = people[arr[i][j] - 1];
                colors[temp - 1] = 1;
            }
        }
        int k = 0;
        for (int j = 0; j < 3; j++) {
            if (colors[j] != 0) {
                continue;
            }
            for (; k < 3; k++) {
                if (people[arr[i][k] - 1] == 0) {
                    people[arr[i][k] - 1] = j + 1;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout<<people[i]<<' ';
    }
    //return 0;
}

int main() {
    int n, m;
    cin>>n>>m;
    int **arr = new int*[m];
    for (int i = 0; i < m; i++) {
        arr[i] = new int[3];
        for (int j = 0; j < 3; j++) {
            cin>>arr[i][j];
        }
    }
    int *people = new int[n];
    for (int i = 0; i < n; i++) {
        people[i] = 0;
    }
    paint(arr, people, n, m);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[] = { 0, 1, 2, 0, 3 };
int notthis(int a) {
    return (~a) & 0b111;
}
int single(int a) {
    for (int i = 0; i < 3; i++)
        if (((a >> i) & 0b1) > 0)
            return 1 << i;
    return 0;
}
int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<int> > notsame(n);
    vector<int> col(n);
    for (int i = 0; i < m; i++) {
        int f, s, t;
        cin >> f >> s >> t;
        f--;
        s--;
        t--;
        notsame[f].push_back(s);
        notsame[f].push_back(t);
        notsame[s].push_back(f);
        notsame[s].push_back(t);
        notsame[t].push_back(f);
        notsame[t].push_back(s);
    }
    for (int i = 0; i < n; i++) {
        if (notsame[i].size() > 2)
            col[i] = 0b001;
    }

    for (int i = 0; i < n; i++) {
        if (notsame[i].size() == 2 && col[i] == 0) {
            int other1 = notsame[i][0];
            int other2 = notsame[i][1];
            col[i] = single(notthis(col[other1]) & notthis(col[other2]));
            if (col[other1] == 0)
                col[other1] = single(notthis(col[i]) & notthis(col[other2]));
            if (col[other2] == 0)
                col[other2] = single(notthis(col[i]) & notthis(col[other1]));
        }
    }
    for (int i = 0; i < n; i++)
        cout << arr[col[i]] << " ";
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[] = { 0, 1, 2, 0, 3 };
int arr2[] = { 0, 1, 0b10, 0b100 };
int notthis(int a) {
    return (~a) & 0b111;
}
int single(int a) {
    for (int i = 0; i < 3; i++)
        if (((a >> i) & 0b1) > 0)
            return 1 << i;
    return 0;
}
int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<int> > notsame(n);
    vector<int> col(n);
    for (int i = 0; i < m; i++) {
        int f, s, t;
        cin >> f >> s >> t;
        f--;
        s--;
        t--;
        notsame[f].push_back(s);
        notsame[f].push_back(t);
        notsame[s].push_back(f);
        notsame[s].push_back(t);
        notsame[t].push_back(f);
        notsame[t].push_back(s);
        col[f] = 0b1;
        col[s] = 0b10;
        col[t] = 0b100;
    }

    for (int i = 0; i < n; i++) {
        if (notsame[i].size() > 2) {
            int m1 = col[notsame[i][0]] | col[notsame[i][1]];
            int m2 = col[notsame[i][2]] | col[notsame[i][3]];

            if ((m1 ^ m2) > 0) {
                if (col[notsame[i][2]] == single(m1 ^ m2))
                    col[notsame[i][2]] = single(
                            (m1 ^ m2) & (~(single(m1 ^ m2))));
                if (col[notsame[i][1]] == single(m1 ^ m2))
                    col[notsame[i][1]] = single(
                            (m1 ^ m2) & (~(single(m1 ^ m2))));
            }
            if(m2^col[i]!=0b111)
            {
                col[i] = single(notthis(m2));
            }
            if(m1^col[i]!=0b111)
            {
                col[i] = single(notthis(m1));
            }
        }
    }
    for (int i = 0; i < n; i++)
        cout << arr[col[i]] << " ";
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[] = { 0, 1, 2, 0, 3 };
int notthis(int a) {
    return (~a) & 0b111;
}
int single(int a) {
    for (int i = 0; i < 3; i++)
        if (((a >> i) & 0b1) > 0)
            return 1 << i;
    return 0;
}
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > notsame(n);
    vector<int> col(n);
    for (int i = 0; i < m; i++) {
        int f, s, t;
        cin >> f >> s >> t;
        f--;
        s--;
        t--;
        notsame[f].push_back(s);
        notsame[f].push_back(t);
        notsame[s].push_back(f);
        notsame[s].push_back(t);
        notsame[t].push_back(f);
        notsame[t].push_back(s);
    }
    for (int i = 0; i < n; i++) {
        if (notsame[i].size() > 2)
            col[i] = 0b001;
    }

    for (int i = 0; i < n; i++) {
        if (notsame[i].size() == 2 && col[i] == 0) {
            int other1 = notsame[i][0];
            int other2 = notsame[i][1];
            col[i] = single(notthis(col[other1]) & notthis(col[other2]));
            if (col[other1] == 0)
                col[other1] = single(notthis(col[i]) & notthis(col[other2]));
            if (col[other2] == 0)
                col[other2] = single(notthis(col[i]) & notthis(col[other1]));
        }
    }
    for (int i = 0; i < n; i++)
        cout << arr[col[i]] << " ";
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[] = { 0, 1, 2, 0, 3 };
int arr2[] = { 0, 1, 0b10, 0b100 };
int notthis(int a) {
    return (~a) & 0b111;
}
int single(int a) {
    for (int i = 0; i < 3; i++)
        if (((a >> i) & 0b1) > 0)
            return 1 << i;
    return 0;
}

vector<vector<int> > notsame;
vector<int> col;
void dfs(int id, int cl) {
    if (col[id])
        return;
    col[id] = cl;
    for (int i = 0; i < notsame[id].size(); i++)
        dfs(notsame[id][i], (cl - 1 + 1) % 3 + 1);
}
int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    col = vector<int>(n);
    notsame = vector<vector<int> >(n);
    for (int i = 0; i < m; i++) {
        int f, s, t;
        cin >> f >> s >> t;
        f--;
        s--;
        t--;
        notsame[f].push_back(s);
        notsame[f].push_back(t);
        notsame[s].push_back(f);
        notsame[s].push_back(t);
        notsame[t].push_back(f);
        notsame[t].push_back(s);
    }

    for (int i = 0; i < n; i++) {
        dfs(i, 1);
    }
    for (int i = 0; i < n; i++)
        cout << col[i] << " ";
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[] = { 0, 1, 2, 0, 3 };
int notthis(int a) {
    return (~a) & 0b111;
}
int single(int a) {
    for (int i = 0; i < 3; i++)
        if (((a >> i) & 0b1) > 0)
            return 1 << i;
    return 0;
}
int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<int> > notsame(n);
    vector<int> col(n);
    for (int i = 0; i < m; i++) {
        int f, s, t;
        cin >> f >> s >> t;
        f--;
        s--;
        t--;
        notsame[f].push_back(s);
        notsame[f].push_back(t);
        notsame[s].push_back(f);
        notsame[s].push_back(t);
        notsame[t].push_back(f);
        notsame[t].push_back(s);
    }

    for (int i = 0; i < n; i++) {
        if (col[i] == 0) {
            int other1 = notsame[i][0];
            int other2 = notsame[i][1];
            col[i] = single(notthis(col[other1]) & notthis(col[other2]));
            if (col[other1] == 0)
                col[other1] = single(notthis(col[i]) & notthis(col[other2]));
            if (col[other2] == 0)
                col[other2] = single(notthis(col[i]) & notthis(col[other1]));
        }
    }
    for (int i = 0; i < n; i++)
        cout << arr[col[i]] << " ";
    cout << endl;
    return 0;
}