/*

 _____________________                              ______________________
 `-._                 \           |\__/|           /                  _.-'
     \                 \          |    |          /                  /
      \                 `-_______/      \_______-'                  /
      |                                                            |
      |                                                            |
      |                                                            |
      /                                                            \
     /_____________                                  _______________\
                   `----._                    _.----'
                          `--.            .--'
                              `-.      .-'
                                 \    /
                                  \  /
                                   \/

 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MM.:  .:'   `:::  .:`MMMMMMMMMMM|`MMM'|MMMMMMMMMMM':  .:'   `:::  .:'.MM
 MMMM.     :          `MMMMMMMMMM  :*'  MMMMMMMMMM'        :        .MMMM
 MMMMM.    ::    .     `MMMMMMMM'  ::   `MMMMMMMM'   .     ::   .  .MMMMM
 MMMMMM. :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: ::.MMMMMM
 MMMMMMM    ;::         ;::         ;::         ;::         ;::   MMMMMMM
 MMMMMMM .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `::MMMMMMM
 MMMMMM'     :           :           :           :           :    `MMMMMM
 MMMMM'______::____      ::    .     ::    .     ::     ___._::____`MMMMM
 MMMMMMMMMMMMMMMMMMM`---._ :: ::'  :   :: ::'  _.--::MMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMM::.         ::  .--MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM-.     ;::-MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM. .:' .M:F_P:MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.   .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\ /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMVMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               __
              //\\
             //  \\
            //    \\
           //      \\
          //        \\
         //          \\
        //____________\\
       //______________\\
      //                \\
     //                  \\
    //                    \\
   //______________________\\
  //________________________\\

 */
#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
//#include <ctime>
using namespace std;

#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long  ll;
typedef long long int  lli;
typedef double d;
typedef string str;
typedef char ch;
#define  mod 1000003
void AWAD(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
}

/*int gcd(int a, int b) {
 return b == 0 ? a : gcd(b, a % b);
 }*/
/*bool cmp (pair <int, int> a, pair <int, int> b){

 if (a.first > b.first)
 return 1;

 return 0;
 }*/
/*ll pr(ll a, ll b){
 ll res = 1;
 for(ll i = 0; i < b; i++)
 {
 res*=a;
 res%=mod;
 }
 return res;
 }*/

/* bool vis[10000001];
 void seive(){
    int i , j;
    for(i=2;i<=10000001;i++)
    if(!vis[i])
        for(j=i+i;j<=10000001;j+=i)
            vis[j] = 1;
 }*/

int main(){

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n, arr[300001];
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
    }
    ll ans = 0;
    sort(arr, arr+n);
    for(int i = 0; i < n; ++i){
        if(i == 0){
            ans+=abs(1-arr[i]);
            arr[i] = 1;
        }
        if(i > 0){
            if(arr[i] <= arr[i-1]){
                int temp = abs(arr[i]-(arr[i-1]+1));
                if(arr[i] != arr[i]+temp){
                    ans+=temp;
                    arr[i] += temp;
                }
            }
            else{
                int temp = abs(arr[i]-(arr[i-1]+1));
                if(arr[i] != arr[i]+temp){
                    ans+=temp;
                    arr[i] -= temp;
                }
            }
        }
       // cout << "ans " << arr[i] << endl;
    }
        cout << ans << endl;
    return 0;
}


/*

 _____________________                              ______________________
 `-._                 \           |\__/|           /                  _.-'
     \                 \          |    |          /                  /
      \                 `-_______/      \_______-'                  /
      |                                                            |
      |                                                            |
      |                                                            |
      /                                                            \
     /_____________                                  _______________\
                   `----._                    _.----'
                          `--.            .--'
                              `-.      .-'
                                 \    /
                                  \  /
                                   \/

 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MM.:  .:'   `:::  .:`MMMMMMMMMMM|`MMM'|MMMMMMMMMMM':  .:'   `:::  .:'.MM
 MMMM.     :          `MMMMMMMMMM  :*'  MMMMMMMMMM'        :        .MMMM
 MMMMM.    ::    .     `MMMMMMMM'  ::   `MMMMMMMM'   .     ::   .  .MMMMM
 MMMMMM. :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: ::.MMMMMM
 MMMMMMM    ;::         ;::         ;::         ;::         ;::   MMMMMMM
 MMMMMMM .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `::MMMMMMM
 MMMMMM'     :           :           :           :           :    `MMMMMM
 MMMMM'______::____      ::    .     ::    .     ::     ___._::____`MMMMM
 MMMMMMMMMMMMMMMMMMM`---._ :: ::'  :   :: ::'  _.--::MMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMM::.         ::  .--MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM-.     ;::-MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM. .:' .M:F_P:MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.   .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\ /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMVMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               __
              //\\
             //  \\
            //    \\
           //      \\
          //        \\
         //          \\
        //____________\\
       //______________\\
      //                \\
     //                  \\
    //                    \\
   //______________________\\
  //________________________\\

 */
#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
//#include <ctime>
using namespace std;

#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long  ll;
typedef long long int  lli;
typedef double d;
typedef string str;
typedef char ch;
#define  mod 1000003
void AWAD(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
}

/*int gcd(int a, int b) {
 return b == 0 ? a : gcd(b, a % b);
 }*/
/*bool cmp (pair <int, int> a, pair <int, int> b){

 if (a.first > b.first)
 return 1;

 return 0;
 }*/
/*ll pr(ll a, ll b){
 ll res = 1;
 for(ll i = 0; i < b; i++)
 {
 res*=a;
 res%=mod;
 }
 return res;
 }*/

/* bool vis[10000001];
 void seive(){
    int i , j;
    for(i=2;i<=10000001;i++)
    if(!vis[i])
        for(j=i+i;j<=10000001;j+=i)
            vis[j] = 1;
 }*/

int main(){

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n, arr[300001];
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
    }
    ll ans = 0;
    for(int i = 0; i < n; ++i){
        if(arr[i] <= 0){
            ans+=(1-arr[i]);
            arr[i] = 1;
        }
        if(i > 0){
            int temp = min(abs(arr[i]-(arr[i-1]+1)), abs(arr[i]-(arr[i-1]-1)));
            if(arr[i] != arr[i-1]+1){
                ans+=temp;
                arr[i] += temp;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


/*

 _____________________                              ______________________
 `-._                 \           |\__/|           /                  _.-'
     \                 \          |    |          /                  /
      \                 `-_______/      \_______-'                  /
      |                                                            |
      |                                                            |
      |                                                            |
      /                                                            \
     /_____________                                  _______________\
                   `----._                    _.----'
                          `--.            .--'
                              `-.      .-'
                                 \    /
                                  \  /
                                   \/

 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MM.:  .:'   `:::  .:`MMMMMMMMMMM|`MMM'|MMMMMMMMMMM':  .:'   `:::  .:'.MM
 MMMM.     :          `MMMMMMMMMM  :*'  MMMMMMMMMM'        :        .MMMM
 MMMMM.    ::    .     `MMMMMMMM'  ::   `MMMMMMMM'   .     ::   .  .MMMMM
 MMMMMM. :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: ::.MMMMMM
 MMMMMMM    ;::         ;::         ;::         ;::         ;::   MMMMMMM
 MMMMMMM .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `::MMMMMMM
 MMMMMM'     :           :           :           :           :    `MMMMMM
 MMMMM'______::____      ::    .     ::    .     ::     ___._::____`MMMMM
 MMMMMMMMMMMMMMMMMMM`---._ :: ::'  :   :: ::'  _.--::MMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMM::.         ::  .--MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM-.     ;::-MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM. .:' .M:F_P:MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.   .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\ /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMVMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               __
              //\\
             //  \\
            //    \\
           //      \\
          //        \\
         //          \\
        //____________\\
       //______________\\
      //                \\
     //                  \\
    //                    \\
   //______________________\\
  //________________________\\

 */
#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
//#include <ctime>
using namespace std;

#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long  ll;
typedef long long int  lli;
typedef double d;
typedef string str;
typedef char ch;
#define  mod 1000003
void AWAD(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
}

/*int gcd(int a, int b) {
 return b == 0 ? a : gcd(b, a % b);
 }*/
/*bool cmp (pair <int, int> a, pair <int, int> b){

 if (a.first > b.first)
 return 1;

 return 0;
 }*/
/*ll pr(ll a, ll b){
 ll res = 1;
 for(ll i = 0; i < b; i++)
 {
 res*=a;
 res%=mod;
 }
 return res;
 }*/

/* bool vis[10000001];
 void seive(){
    int i , j;
    for(i=2;i<=10000001;i++)
    if(!vis[i])
        for(j=i+i;j<=10000001;j+=i)
            vis[j] = 1;
 }*/

int main(){

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n, arr[300001], ar[300001];
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
        ar[i] = arr[i];
    }
    ll ans = 0;
    for(int i = 0; i < n; ++i){
        if(arr[i] <= 0){
            ans+=(1-arr[i]);
            arr[i] = 1;
        }
        if(i > 0){
            int temp = abs(arr[i]-(arr[i-1]+1));
            if(arr[i] != arr[i]+temp){
                ans+=temp;
                arr[i] += temp;
            }
        }
    }
    ll ans2 = 0;
    for(int i = 0; i < n; ++i){
        if(ar[i] <= 0){
            ans2+=(1-ar[i]);
            ar[i] = 1;
        }
        if(i > 0){
            int temp = abs(ar[i]-(ar[i-1]-1));
            if(ar[i] != ar[i]+temp){
                ans2+=temp;
                ar[i] += temp;
            }
        }
    }
 //   cout << "ans = " << ans << " ans 2 = " << ans2 << endl;
    cout << min(ans, ans2) << endl;
    return 0;
}


/*

 _____________________                              ______________________
 `-._                 \           |\__/|           /                  _.-'
     \                 \          |    |          /                  /
      \                 `-_______/      \_______-'                  /
      |                                                            |
      |                                                            |
      |                                                            |
      /                                                            \
     /_____________                                  _______________\
                   `----._                    _.----'
                          `--.            .--'
                              `-.      .-'
                                 \    /
                                  \  /
                                   \/

 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MM.:  .:'   `:::  .:`MMMMMMMMMMM|`MMM'|MMMMMMMMMMM':  .:'   `:::  .:'.MM
 MMMM.     :          `MMMMMMMMMM  :*'  MMMMMMMMMM'        :        .MMMM
 MMMMM.    ::    .     `MMMMMMMM'  ::   `MMMMMMMM'   .     ::   .  .MMMMM
 MMMMMM. :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: ::.MMMMMM
 MMMMMMM    ;::         ;::         ;::         ;::         ;::   MMMMMMM
 MMMMMMM .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `::MMMMMMM
 MMMMMM'     :           :           :           :           :    `MMMMMM
 MMMMM'______::____      ::    .     ::    .     ::     ___._::____`MMMMM
 MMMMMMMMMMMMMMMMMMM`---._ :: ::'  :   :: ::'  _.--::MMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMM::.         ::  .--MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM-.     ;::-MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM. .:' .M:F_P:MMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.   .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\ /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMVMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               __
              //\\
             //  \\
            //    \\
           //      \\
          //        \\
         //          \\
        //____________\\
       //______________\\
      //                \\
     //                  \\
    //                    \\
   //______________________\\
  //________________________\\

 */
#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
//#include <ctime>
using namespace std;

#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long  ll;
typedef long long int  lli;
typedef double d;
typedef string str;
typedef char ch;
#define  mod 1000003
void AWAD(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
}

/*int gcd(int a, int b) {
 return b == 0 ? a : gcd(b, a % b);
 }*/
/*bool cmp (pair <int, int> a, pair <int, int> b){

 if (a.first > b.first)
 return 1;

 return 0;
 }*/
/*ll pr(ll a, ll b){
 ll res = 1;
 for(ll i = 0; i < b; i++)
 {
 res*=a;
 res%=mod;
 }
 return res;
 }*/

/* bool vis[10000001];
 void seive(){
    int i , j;
    for(i=2;i<=10000001;i++)
    if(!vis[i])
        for(j=i+i;j<=10000001;j+=i)
            vis[j] = 1;
 }*/

int main(){

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n, arr[300001], ar[300001];
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
        ar[i] = arr[i];
    }
    ll ans = 0;
    for(int i = 0; i < n; ++i){
        if(arr[i] <= 0){
            ans+=(1-arr[i]);
            arr[i] = 1;
        }
        if(i > 0){
            int temp = abs(arr[i]-(arr[i-1]+1));
            if(arr[i] != arr[i]+temp){
                ans+=temp;
                arr[i] += temp;
            }
        }
       // cout << "ans " << arr[i] << endl;
    }
    ll ans2 = 0;
    for(int i = 0; i < n; ++i){
        if(ar[i] <= 0){
            ans2+=(1-ar[i]);
            ar[i] = 1;
        }
        if(i > 0){
            int temp = abs(ar[i]-(ar[i-1]-1));
            if(ar[i] != ar[i]+temp){
                ans2+=temp;
                ar[i] -= temp;
                if(arr[i] <= 0){
                    cout << ans << endl;
                    return 0;
                }
            }
        }
        // cout << "ans2 " << ar[i] << endl;
    }
  //  cout << "ans = " << ans << " ans 2 = " << ans2 << endl;
    cout << min(ans, ans2) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr, arr+n);
    int ans=0;
    int k=0;
    for(int i=n-1;i>=0;i--)
    {
        int h=arr[i]-n-k;
        h=abs(h);
        k++;
        ans=ans+h;
    }
    cout<<ans/2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr, arr+n);
    long long int ans=0;
    int k=0;
    for(int i=n-1;i>=0;i--)
    {
       if(arr[i]>n)
       {
          int h=arr[i]-n-k;
          k++;
          ans=ans+h;
       }
       else
       {
        int h=arr[i]+k;
        k++;
        h=abs(h);
        ans=ans+h;
       }
    }
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr, arr+n);
    long long int ans=0;
    int k=0;
    for(int i=n-1;i>=0;i--)
    {
       if(arr[i]>n)
       {
          int h=arr[i]-n-k;
          k++;
          ans=ans+h;
       }
       else
       {
        int h=arr[i]-n+k;
        k++;
        h=abs(h);
        ans=ans+h;
       }
    }
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr, arr+n);
    long long int ans=0;
    int k=0;
    for(int i=n-1;i>=0;i--)
    {
       int h=arr[i]-n-k;
       h=abs(h);
       ans=ans+h;
       k++;
    }
    cout<<ans/2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr, arr+n);
    long long int ans=0;
    int k=1;
    for(int i=0;i<n;i++)
    {
       int h=arr[i]-k;
       h=abs(h);
       ans=ans+h;
       k++;
    }
    cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int mod(int n){
    if(n>0)return n;
    else return -1*n;
}
int main(){
int n;
cin>>n;
vector<long long int >v(n);
for(int i=0;i<n;i++)cin>>v[i];
sort(v.begin(),v.end());
long long int k=0;
for(int i=0;i<n;i++){
    v[i]=i;
    k+=mod(v[i]-i);
}
cout<<k;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int mod(int n){
    if(n>0)return n;
    else return -1*n;
}
int main(){
int n;
cin>>n;
vector<long long int >v(n);
for(int i=0;i<n;i++)cin>>v[i];
sort(v.begin(),v.end());
long long int k=0;
for(int i=0;i<n;i++){
    k+=mod(v[i]-i-1);
}
cout<<k;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int mod(int n){
    if(n>0)return n;
    else return -1*n;
}
int main(){
int n;
cin>>n;
vector<long long int >v(n);
for(int i=0;i<n;i++)cin>>v[i];
sort(v.begin(),v.end());
long long int k=0;
for(int i=0;i<n;i++){
    v[i]=i+1;
    k+=mod(v[i]-i-1);
}
cout<<k;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int mod(int n){
    if(n>0)return n;
    else return -1*n;
}
int main(){
int n;
cin>>n;
vector<long long int >v(n);
for(int i=0;i<n;i++)cin>>v[i];
sort(v.begin(),v.end());
long long int k=0;
for(int i=0;i<n;i++){
    v[i]=i;
    k+=v[i]-i;
}
cout<<k;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<long long int >v(n);
for(int i=0;i<n;i++)cin>>v[i];
sort(v.begin(),v.end());
long long int k=0;
for(int i=0;i<n;i++){
    v[i]=i;
    k+=v[i]-i;
}
cout<<k;
return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n;
    cin>>n;
    long long int a[n];
    int ans=0;
    for (long long int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a, a+n);
    for (long long int i=0; i<n; i++)
    {
        ans=ans+abs(i+1-a[i]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n;
    cin>>n;
    long long int a[n+1];
    int ans=0;
    for (long long int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    a[0]=-10000000000;
    sort(a, a+n+1);
    for (long long int i=1; i<=n; i++)
    {
        ans=ans+(i-a[i]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long long int a[n+1];
    int ans=0;
    for (int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    a[0]=-10000000000;
    sort(a, a+n);
    for (int i=1; i<=n; i++)
    {
        ans=ans+(i-a[i]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n;
    cin>>n;
    long long int a[n+1];
    int ans=0;
    for (long long int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    a[0]=-10000000000;
    sort(a, a+n+1);
    for (long long int i=1; i<=n; i++)
    {
        if(i>a[i])
        {
            ans=ans+(i-a[i]);
        }
        if(i<a[i])
        {
            ans=ans+(a[i]-i);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int n;
    cin>>n;
    long long int a[n];
    long long int ans=0;
    for (long long int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a, a+n);
    for (long long int i=0; i<n; i++)
    {
        ans=ans+abs(i+1-a[i]);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 long int n,count=0;
 cin>>n;
 long int a[n+1];
 a[0]=-(1e9+7);
 for(long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n+1);
 for(long int i=1;i<=n;i++)
 count+=(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 long long int n;
 long long int count=0;
 cin>>n;
 long long int a[n+1];
 a[0]=-(1e9+7);
 for(long long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n+1);
 for(long long int i=1;i<=n;i++)
 count+=abs(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 long int n,count=0;
 cin>>n;
 long int a[n+1];
 a[0]=-(1e9+7);
 for(long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n+1);
 for(long int i=1;i<=n;i++)
 count+=abs(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n,count=0;
 cin>>n;
 long int a[n+1];
 a[0]=-(1e9+7);
 for(long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n);
 for(long int i=1;i<=n;i++)
 count+=(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 long long int n,count=0;
 cin>>n;
 long long int a[n+1];
 a[0]=-(1e9+7);
 for(long long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n+1);
 for(long long int i=1;i<=n;i++)
 count+=abs(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n,count=0;
 cin>>n;
 long int a[n+1];
 a[0]=-(1e9+7);
 for(long int i=1;i<=n;i++)
 cin>>a[i];
 sort(a,a+n+1);
 for(long int i=1;i<=n;i++)
 count+=(i-a[i]);
 cout<<count;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long

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

	ll arr[n+1];

	for(int i=1;i<=n;i++)
	{
		cin>>arr[i];

	}
	sort(arr+1,arr+n+1);

	ll sum=0;

	for(int i=1;i<=n;i++)
	{
		sum+=abs(i-arr[i]);
	}
	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int arr[n];

	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);
	long long int sum=0;
	for(int i=0;i<n;i++)
	{
		int x=abs(arr[i]-(i+1));
		sum+=x;

	}
	cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int arr[n];
	unordered_map<int,int>m;
	for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(m.count(arr[i])==0)
				m.insert({arr[i],1});
			else
				m[arr[i]]++;
		}
	long long sum=0;
	sort(arr,arr+n);

	unordered_set<int> ans;

int j=0;
	for(int i=1;i<=n;i++)
	{
		if(ans.count(i)==0)
		{
			if(m.count(i))
				{
					ans.insert(i);
					m.erase(i);
				}
				else if(m.count(arr[j]))
				{
					int x=abs(arr[j]-i);
					sum+=x;
					if(m[arr[j]]==1)
					m.erase(arr[j]);
					else
					m[arr[j]]--;
					ans.insert(i);
					j++;
				}
		}
	}
	cout<<sum;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll n;
	cin>>n;
	ll arr[n];
	unordered_map<ll,ll>m;
	for(ll i=0;i<n;i++)
		{
			cin>>arr[i];
			if(m.count(arr[i])==0)
				m.insert({arr[i],1});
			else
				m[arr[i]]++;
		}
	long long sum=0;
	sort(arr,arr+n);

	unordered_set<ll> ans;

ll j=0;
	for(ll i=1;i<=n;i++)
	{
		if(ans.count(i)==0)
		{
			if(m.count(i))
				{
					ans.insert(i);
					m.erase(i);
				}
				else if(m.count(arr[j]))
				{
					ll x=abs(arr[j]-i);
					sum+=x;
					if(m[arr[j]]==1)
					m.erase(arr[j]);
					else
					m[arr[j]]--;
					ans.insert(i);
					j++;
				}
		}
	}
	cout<<sum;

}


#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[250000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i + 1 - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[100000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n +1);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i + 1 - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[200000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i + 1 - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[300000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n+1);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[200000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n+1);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i  - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n, a[100000];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n +1);

    long long result(0);
    for (int i = 0; i < n; ++i)
    {
        result += abs(i - a[i]);
    }
    printf("%I64d", result);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    int N;
    scanf("%d", &N);

    vector<ll> v(N, 0);
    for (ll& i : v)
        scanf("%lld", &i);

    sort(v.begin(), v.end());

    ll ans = 0;
    for (int i = 0; i < N; i++) {
        ans += abs((i + 1) - v[i]);
    }

    printf("%lld\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long i,a[100001],n,c=0;

int main()
{

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

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

    cout<<c;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int i,a[100001],n,c=0,sum=0;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

   cout<<c;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long int i,a[100001],n,c=0,sum=0;
    scanf("%ld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%ld",&a[i]);
    }

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

    printf("%ld",c);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long int i,a[100001],n,c=0,sum=0;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

    printf("%lld",c);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long i,a[100001],n,c=0,sum=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

   cout<<c;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long i,a[300005],n,c=0;

int main()
{

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

    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        c+=abs(a[i]-(i+1));
    }

    cout<<c;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
using namespace std;

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

   for(int i=0;i<n;i++)
   {
       scanf("%d",&a[i])
   }
    sort(a,a+n);

    long long int d=0;

    for(int i=0;i<n;i++)
    {
        d+=abs(a[i]-i-1);
    }
    cout<<d;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;

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

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

    long long int d=0;

    for(int i=0;i<n;i++)
    {
        d+=abs(a[i]-i-1);
    }
    cout<<d;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

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

   for(int i=0;i<n;i++)
   {
       scanf("%d",&a[i]);
   }
    sort(a,a+n);

    long long int d=0;

    for(int i=0;i<n;i++)
    {
        d+=abs(a[i]-i-1);
    }
    cout<<d;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

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

   for(int i=0;i<n;i++)
   {
       scanf("%d",&a[i]);
   }
    sort(a,a+n);

    long long int d=0;

    for(int i=0;i<n;i++)
    {
        d+=abs(a[i]-i-1);
    }
    cout<<d;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

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

   for(int i=0;i<n;i++)
   {
       scanf("%d",&a[i]);
   }
    sort(a,a+n);

    long long int d=0;

    for(int i=0;i<n;i++)
    {
        d+=abs(a[i]-i-1);
    }
    cout<<d;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

long find_minimum_moves (std::vector<int> elements) {

    long num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (int i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    int n;
    std::vector<int> array;

    std::cin >> n;
    array.resize(n);

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

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

int find_minimum_moves (std::vector<int> elements) {

    int num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (int i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    int n;
    std::vector<int> array;

    std::cin >> n;
    array.resize(n);

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

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

long find_minimum_moves (std::vector<long> elements) {

    long num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (std::size_t i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    std::size_t n;
    std::vector<long> array;

    std::cin >> n;
    array.resize(n);

    for (std_size t i = 0; i < n; i++) {
        std::cin >> array[i];
    }

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

long find_minimum_moves (std::vector<long> elements) {

    long num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (std::size_t i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    std::size_t n;
    std::vector<long> array;

    std::cin >> n;
    array.resize(n);

    for (std::size_t i = 0; i < n; i++) {
        std::cin >> array[i];
    }

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

int find_minimum_moves (std::vector<int> elements) {

    int num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (int i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    int n;
    vector<int> array;

    std::cin >> n;
    array.resize(n);

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

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

long long find_minimum_moves (std::vector<long> elements) {

    long long num_moves = 0;
    std::sort(elements.begin(), elements.end());

    for (std::size_t i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    std::size_t n;
    std::vector<long> array;

    std::cin >> n;
    array.resize(n);

    for (std::size_t i = 0; i < n; i++) {
        std::cin >> array[i];
    }

    std::cout << find_minimum_moves(array) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

long find_minimum_moves (std::vector<long> elements) {

    long num_moves = 0;

    std::sort(elements.begin(), elements.end());

    for (std::size_t i = 0; i < elements.size(); i++) {
        num_moves += abs(i + 1 - elements[i]);
    }

    return num_moves;
}

int main () {

    std::size_t n;
    std::vector<long> array;

    std::cin >> n;
    array.resize(n);

    for (std::size t i = 0; i < n; i++) {
        std::cin >> array[i];
    }

    std::cout << find_minimum_moves(array) << std::endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
main()
{
    ll n,i,sum=0;
    cin>>n;
    ll a[n+5];
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n);
    for (i=1; i<=n; i++)
    {
    sum+=abs(i-a[i]);

}
 cout<<sum<<endl;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
main()
{
    ll n,i,sum=0;
    cin>>n;
    ll a[n];
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for (i=1; i<=n; i++)
    {
    sum+=abs(i-a[i]);

}
 cout<<sum<<endl;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
main()
{
    ll n,i,sum=0;
    cin>>n;
    ll a[n+5];
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for (i=1; i<=n; i++)
    {
    sum+=abs(i-a[i]);

}
 cout<<sum<<endl;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
main()
{
    ll n,i,sum=0;
    cin>>n;
    ll a[n];
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for (i=1; i<=n; i++)
    {
    sum+=abs(i-a[i]);

}
 cout<<sum<<endl;

}


#pragma GCC optimize(2,3,"Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
	char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
	in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
	in ll read()
	{
		ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
		x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
	}
	in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
	in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
	in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
	in void flush() {if(C>1<<22) ot();}
	template <typename T>
	in void write(T x,char t)
	{
		int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
		if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
	}
	in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=3e5+5;
int n,m,tot,ch[N][26],vis[N][26],fail[N],cnt[N],sum[N],rt[N],siz[N];
char s[N];
struct acam
{
	int st[N],t;
	int merge(int x,int y)
	{
		if(!x||!y) return x+y;
		for(int i=0;i<26;i++) ch[x][i]=merge(ch[x][i],ch[y][i]);
		return cnt[x]+=cnt[y],x;
	}
	in void build(int id)
	{
		static int q[N*10];int l=0,r=0;
		for(int i=0;i<26;i++)
		if(ch[rt[id]][i]) fail[vis[rt[id]][i]=ch[rt[id]][i]]=rt[id],q[++r]=vis[rt[id]][i];
		else vis[rt[id]][i]=rt[id];
		while(l<r)
		{
			int u=q[++l];sum[u]=cnt[u]+sum[fail[u]];
			for(int i=0;i<26;i++)
			if(ch[u][i]) fail[vis[u][i]=ch[u][i]]=vis[fail[u]][i],q[++r]=vis[u][i];
			else vis[u][i]=vis[fail[u]][i];
		}
	}//a
	in int ask(char *s)
	{
		int n=strlen(s+1),res=0;
		for(int i=1;i<=t;i++) for(int j=1,u=rt[st[i]];j<=n;j++) u=vis[u][s[j]-'a'],res+=sum[u];
		return res;
	}
	in void ins(char *s)
	{
		int n=strlen(s+1),id=++tot;if(!rt[id]) rt[id]=++m;
		int p=rt[id];for(int i=1;i<=n;i++) ch[p][s[i]-'a']=++m,p=ch[p][s[i]-'a'];
		cnt[p]++;siz[id]=1;while(t&&siz[st[t]]==siz[id]) rt[id]=merge(rt[id],rt[st[t]]),siz[id]+=siz[st[t--]];
		build(st[++t]=id);
	}
}t1,t2;
int main()
{
	n=read();
	while(n--)
	{
		int op=read();gs(s+1);
		if(op==1) t1.ins(s);
		else if(op==2) t2.ins(s);
		else write(t1.ask(s)-t2.ask(s),'\n');
		fflush(stdout);memset(s,0,sizeof(char)*(strlen(s+1)+1));
	}
	return ot(),0;
}
//Author: disangan233
//In my dream's scene,I can see the everything that in Cyaegha.


#pragma GCC optimize(2,3,"Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
	char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
// 	in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
	#define gc getchar
	in ll read()
	{
		ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
		x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
	}
	in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
	in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
	in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
	in void flush() {if(C>1<<22) ot();}
	template <typename T>
	in void write(T x,char t)
	{
		int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
		if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
	}
	in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=3e5+5;
int n,m,tot,ch[N][26],vis[N][26],fail[N],cnt[N],sum[N],rt[N],siz[N];
char s[N];
struct acam
{
	int st[N],t;
	int merge(int x,int y)
	{
		if(!x||!y) return x+y;
		for(int i=0;i<26;i++) ch[x][i]=merge(ch[x][i],ch[y][i]);
		return cnt[x]+=cnt[y],x;
	}
	in void build(int id)
	{
		static int q[N*10];int l=0,r=0;
		for(int i=0;i<26;i++)
		if(ch[rt[id]][i]) fail[vis[rt[id]][i]=ch[rt[id]][i]]=rt[id],q[++r]=vis[rt[id]][i];
		else vis[rt[id]][i]=rt[id];
		while(l<r)
		{
			int u=q[++l];sum[u]=cnt[u]+sum[fail[u]];
			for(int i=0;i<26;i++)
			if(ch[u][i]) fail[vis[u][i]=ch[u][i]]=vis[fail[u]][i],q[++r]=vis[u][i];
			else vis[u][i]=vis[fail[u]][i];
		}
	}
	in int ask(char *s)
	{
		int n=strlen(s+1),res=0;
		for(int i=1;i<=t;i++) for(int j=1,u=rt[st[i]];j<=n;j++) u=vis[u][s[j]-'a'],res+=sum[u];
		return res;
	}
	in void ins(char *s)
	{
		int n=strlen(s+1),id=++tot;if(!rt[id]) rt[id]=++m;
		int p=rt[id];for(int i=1;i<=n;i++) ch[p][s[i]-'a']=++m,p=ch[p][s[i]-'a'];
		cnt[p]++;siz[id]=1;while(t&&siz[st[t]]==siz[id]) rt[id]=merge(rt[id],rt[st[t]]),siz[id]+=siz[st[t--]];
		build(st[++t]=id);
	}
}t1,t2;
int main()
{
	n=read();
	while(n--)
	{
		int op=read();gs(s+1);
		if(op==1) t1.ins(s);
		else if(op==2) t2.ins(s);
		else printf("%d\n",t1.ask(s)-t2.ask(s));
		fflush(stdout);memset(s,0,sizeof(char)*(strlen(s+1)+1));
	}
	return ot(),0;
}
//Author: disangan233
//In my dream's scene,I can see the everything that in Cyaegha.


#pragma GCC optimize(2,3,"Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
	char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
// 	in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
	#define gc getchar
	in ll read()
	{
		ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
		x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
	}
	in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
	in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
	in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
	in void flush() {if(C>1<<22) ot();}
	template <typename T>
	in void write(T x,char t)
	{
		int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
		if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
	}
	in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=3e5+5;
int n,m,tot,ch[N][26],vis[N][26],fail[N],cnt[N],sum[N],rt[N],siz[N];
char s[N];
struct acam
{
	int st[N],t;
	int merge(int x,int y)
	{
		if(!x||!y) return x+y;
		for(int i=0;i<26;i++) ch[x][i]=merge(ch[x][i],ch[y][i]);
		return cnt[x]+=cnt[y],x;
	}
	in void build(int id)
	{
		static int q[N*10];int l=0,r=0;
		for(int i=0;i<26;i++)
		if(ch[rt[id]][i]) fail[vis[rt[id]][i]=ch[rt[id]][i]]=rt[id],q[++r]=vis[rt[id]][i];
		else vis[rt[id]][i]=rt[id];
		while(l<r)
		{
			int u=q[++l];sum[u]=cnt[u]+sum[fail[u]];
			for(int i=0;i<26;i++)
			if(ch[u][i]) fail[vis[u][i]=ch[u][i]]=vis[fail[u]][i],q[++r]=vis[u][i];
			else vis[u][i]=vis[fail[u]][i];
		}
	}
	in int ask(char *s)
	{
		int n=strlen(s+1),res=0;
		for(int i=1;i<=t;i++) for(int j=1,u=rt[st[i]];j<=n;j++) u=vis[u][s[j]-'a'],res+=sum[u];
		return res;
	}
	in void ins(char *s)
	{
		int n=strlen(s+1),id=++tot;if(!rt[id]) rt[id]=++m;
		int p=rt[id];for(int i=1;i<=n;i++) ch[p][s[i]-'a']=++m,p=ch[p][s[i]-'a'];
		cnt[p]++;siz[id]=1;while(t&&siz[st[t]]==siz[id]) rt[id]=merge(rt[id],rt[st[t]]),siz[id]+=siz[st[t--]];
		build(st[++t]=id);
	}
}t1,t2;
int main()
{
	n=read();
	while(n--)
	{
		int op=read();gs(s+1);
		if(op==1) t1.ins(s);
		else if(op==2) t2.ins(s);
		else write(t1.ask(s)-t2.ask(s),'\n');
		fflush(stdout);memset(s,0,sizeof(char)*(strlen(s+1)+1));
	}
	return ot(),0;
}
//Author: disangan233
//In my dream's scene,I can see the everything that in Cyaegha.


#pragma GCC optimize(2,3,"Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
	char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
	in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
	in ll read()
	{
		ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
		x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
	}
	in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
	in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
	in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
	in void flush() {if(C>1<<22) ot();}
	template <typename T>
	in void write(T x,char t)
	{
		int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
		if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
	}
	in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=3e5+5;
int n,m,tot,ch[N][26],vis[N][26],fail[N],cnt[N],sum[N],rt[N],siz[N];
char s[N];
struct acam
{
	int st[N],t;
	int merge(int x,int y)
	{
		if(!x||!y) return x+y;
		for(int i=0;i<26;i++) ch[x][i]=merge(ch[x][i],ch[y][i]);
		return cnt[x]+=cnt[y],x;
	}
	in void build(int id)
	{
		static int q[N*10];int l=0,r=0;
		for(int i=0;i<26;i++)
		if(ch[rt[id]][i]) fail[vis[rt[id]][i]=ch[rt[id]][i]]=rt[id],q[++r]=vis[rt[id]][i];
		else vis[rt[id]][i]=rt[id];
		while(l<r)
		{
			int u=q[++l];sum[u]=cnt[u]+sum[fail[u]];
			for(int i=0;i<26;i++)
			if(ch[u][i]) fail[vis[u][i]=ch[u][i]]=vis[fail[u]][i],q[++r]=vis[u][i];
			else vis[u][i]=vis[fail[u]][i];
		}
	}
	in int ask(char *s)
	{
		int n=strlen(s+1),res=0;
		for(int i=1;i<=t;i++) for(int j=1,u=rt[st[i]];j<=n;j++) u=vis[u][s[j]-'a'],res+=sum[u];
		return res;
	}
	in void ins(char *s)
	{
		int n=strlen(s+1),id=++tot;if(!rt[id]) rt[id]=++m;
		int p=rt[id];for(int i=1;i<=n;i++) ch[p][s[i]-'a']=++m,p=ch[p][s[i]-'a'];
		cnt[p]++;siz[id]=1;while(t&&siz[st[t]]==siz[id]) rt[id]=merge(rt[id],rt[st[t]]),siz[id]+=siz[st[t--]];
		build(st[++t]=id);
	}
}t1,t2;
int main()
{
	n=read();
	while(n--)
	{
		int op=read();gs(s+1);
		if(op==1) t1.ins(s);
		else if(op==2) t2.ins(s);
		else write(t1.ask(s)-t2.ask(s),'\n');
		fflush(stdout);memset(s,0,sizeof(char)*(strlen(s+1)+1));
	}
	return ot(),0;
}
//Author: disangan233
//In my dream's scene,I can see the everything that in Cyaegha.


//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out = 0, fh = 1;
	char jp = getchar();
	while ((jp > '9' || jp < '0') && jp != '-')
		jp = getchar();
	if (jp == '-')
		fh = -1, jp = getchar();
	while (jp >= '0' && jp <= '9')
		out = out * 10 + jp - '0', jp = getchar();
	return out * fh;
}
void print(ll x)
{
	if (x >= 10)
		print(x / 10);
	putchar('0' + x % 10);
}
void write(ll x, char c)
{
	if (x < 0)
		putchar('-'), x = -x;
	print(x);
	putchar(c);
}
const int N = 3e5 + 10, S = 26;
struct ACAutoMaton
{
	int ch[N][S], tr[N][S], rt[N], siz[N], tp = 0, idx = 0, fail[N], val[N], tot[N];
	int merge(int x, int y)
	{
		if (!x || !y)
			return x + y;
		val[x] += val[y];
		for (int i = 0; i < S; ++i)
			ch[x][i] = merge(ch[x][i], ch[y][i]);
		return x;
	}
	void ins(string s, int n)
	{
		rt[++tp] = ++idx, siz[tp] = 1;
		int x = rt[tp];
		for (int i = 0; i < n; ++i)
		{
			int c = s[i] - 'a';
			if (!ch[x][c])
				ch[x][c] = ++idx;
			x = ch[x][c];
		}
		++val[x];
		while (tp > 1 && siz[tp] == siz[tp - 1])
		{
			rt[tp - 1] = merge(rt[tp - 1], rt[tp]);
			siz[tp - 1] += siz[tp];
			--tp;
		}
		getfail(rt[tp]);
	}
	queue<int> q;
	void getfail(int x)
	{
		for (int i = 0; i < S; ++i)
			if (ch[x][i])
			{
				fail[tr[x][i] = ch[x][i]] = x;
				q.push(tr[x][i]);
			}
			else
				tr[x][i] = x;
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			for (int i = 0; i < S; ++i)
				if (ch[u][i])
				{
					tr[u][i] = ch[u][i];
					fail[tr[u][i]] = tr[fail[u]][i];
					q.push(tr[u][i]);
				}
				else
					tr[u][i] = tr[fail[u]][i];
			tot[u] = val[u] + tot[fail[u]];
		}
	}
	ll query(string s, int n)
	{
		ll res = 0;
		for (int i = 1; i <= tp; ++i)
			for (int j = 0, x = rt[i]; j < n; ++j)
			{
				int c = s[j] - 'a';
				x = tr[x][c];
				res += tot[x];
			}
		return res;
	}
} Ins, Del;
int m, op;
string buf;
int main()
{
	m = read();
	for (int i = 1; i <= m; ++i)
	{
		cin >> op >> buf;
		int n = buf.length();
		if (op == 1)
			Ins.ins(buf, n);
		else if (op == 2)
			Del.ins(buf, n);
		else
		{
			ll ans = Ins.query(buf, n) - Del.query(buf, n);
			write(ans, '\n');
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;

#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)

const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif

#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
const int K = 26; // alphabet size
struct aho{
    struct node{  // trie node
        array<int, K> next;
        int fail = -1;
        int ends_here = -1;
        ll num_match = 0;
        node(){
            fill(all(next), -1);
        }
    };
    vector<string> strings;
    vector<node> trie;
    aho(){trie.resize(1);}

    void add_string(string s){
        strings.push_back(s);
        int v = 0;
        for(char ch : s){
            int c = ch - 'a';
            if(trie[v].next[c] == -1){
                trie[v].next[c] = trie.size();
                trie.push_back(node());
            }
            v = trie[v].next[c];
        }
        trie[v].ends_here = strings.size()-1;
    }
    void construct_automaton(){
        queue<int> q; q.push(0);
        while(!q.empty()){
            int v = q.front(); q.pop();
            REP(i, 26){
                if(trie[v].next[i] != -1){
                    int back = trie[v].fail;
                    while(back >= 0 && trie[back].next[i] == -1) back = trie[back].fail;
                    int failure = (back == -1 ? 0 : trie[back].next[i]);
                    trie[trie[v].next[i]].fail = failure;
                    trie[trie[v].next[i]].num_match = (trie[trie[v].next[i]].ends_here != -1 ? 1 + trie[failure].num_match : trie[failure].num_match);
                    q.push(trie[v].next[i]);
                }
            }
        }
    }
    ll match_words(string text){
        int v = 0;
        ll matched = 0;
        REP(i, text.length()){
            while(v >= 0 && trie[v].next[text[i]-'a'] == -1) v = trie[v].fail;
            if(v == -1) v = 0;
            else v = trie[v].next[text[i] - 'a'];
            matched += trie[v].num_match;
            /* int out = v; */
            /* while(out != -1){ */
            /*     if(trie[out].ends_here != -1) matched.pb(mp(trie[out].ends_here, i-strings[trie[out].ends_here].length()+1)); */
            /*     out = trie[out].last_match; */
            /* } */
        }
        return matched;
    }
};

int main(){
 	ios_base::sync_with_stdio(false);
 	cin.tie(NULL); cout.tie(NULL);
	int m; cin >> m;
    vector<set<string>> add(20);
    vector<set<string>> rem(20);
    vector<aho> add_aho(20);
    vector<aho> rem_aho(20);
    REP(i, 20) add_aho[i].construct_automaton();
    REP(i, 20) rem_aho[i].construct_automaton();
    while(m--){
        int type; cin >> type;
        string s; cin >> s;
        if(type == 1){
            add[0].insert(s);
            int pp = 0;
            while(add[pp].size() == (1 << pp)){
                for(auto x : add[pp]) add[pp+1].insert(x);
                add[pp].clear();
                add_aho[pp] = aho();
                add_aho[pp].construct_automaton();
                add_aho[pp+1] = aho();
                for(auto x : add[pp+1]) add_aho[pp+1].add_string(x);
                add_aho[pp+1].construct_automaton();
                pp++;
            }
            //dbg(add_aho[1].strings)
        }
        else if(type == 2){
            rem[0].insert(s);
            int pp = 0;
            while(rem[pp].size() == (1 << pp)){
                for(auto x : rem[pp]) rem[pp+1].insert(x);
                rem[pp].clear();
                rem_aho[pp] = aho();
                rem_aho[pp].construct_automaton();
                rem_aho[pp+1] = aho();
                for(auto x : rem[pp+1]) rem_aho[pp+1].add_string(x);
                rem_aho[pp+1].construct_automaton();
                pp++;
            }
        }
        else{
            ll ans_add = 0;
            ll ans_rem = 0;
            REP(i, 20){
                ans_add += add_aho[i].match_words(s);
                ans_rem += rem_aho[i].match_words(s);
            }
            cout << ans_add - ans_rem << "\n";
            cout.flush();
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 3e5 + 200;
const int W = 2500;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        int j = 0;
        for(int k = 1; k <= m; k ++){
          while(j > 0 && longs[i][j] != longs[i][k])
            j = p[j - 1];
          if(longs[i][j] == longs[i][k])
            ++ j;
          p[k] = j;
        }
        j = 0;
        for(int k = 0; k < n; k ++){
          while(j > 0 && s[k] != longs[i][j])
            j = p[j - 1];
          if(s[k] == longs[i][j])
            ++ j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20000;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = (int)all[i].second.size() - 1;
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[sz + k];
          while(j > 0 && f[j] != s[k])
            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k + sz + 1] = j;
          if(pf[k + sz + 1] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 5e5 + 200;
const int W = 500;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        int j = 0;
        for(int k = 1; k <= m; k ++){
          while(j > 0 && longs[i][j] != longs[i][k])
            j = p[j - 1];
          if(longs[i][j] == longs[i][k])
            ++ j;
          p[k] = j;
        }
        j = 0;
        for(int k = 0; k < n; k ++){
          while(j > 0 && s[k] != longs[i][j])
            j = p[j - 1];
          if(s[k] == longs[i][j])
            ++ j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 5e5 + 200;
const int W = 500;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        for(int k = 1; k <= m; k ++){
          int j = p[k - 1];
          while(j > 0 && longs[i][k] != longs[i][j])
            j = p[j - 1];
          if(longs[i][k] == longs[i][j])
            ++ j;
          p[k] = j;
        }
        for(int k = 0; k < n; k ++){
          int j = p[m + k];
          while(j > 0 && longs[i][j] != s[k])
            j = p[j - 1];
          if(longs[i][j] == s[k])
            ++ j;
          p[m + k] = j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = (int)all[i].second.size() - 1;
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[sz + k];
          while(j > 0 && f[j] != s[k])
            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k + sz + 1] = j;
          if(pf[k + sz + 1] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[m] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] == sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 3e5 + 200;
const int W = 500;

int go[N][29], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "{";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        for(int k = 1; k <= m; k ++){
          int j = p[k - 1];
          while(j > 0 && longs[i][k] != longs[i][j])
            j = p[j - 1];
          if(longs[i][k] == longs[i][j])
            ++ j;
          p[k] = j;
        }
        for(int k = 0; k < n; k ++){
          int j = p[m + k];
          while(j > 0 && longs[i][j] != s[k])
            j = p[j - 1];
          if(longs[i][j] == s[k])
            ++ j;
          p[m + k] = j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = (int)all[i].second.size() - 1;
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[sz];
//          while(j > 0 && f[j] != s[k])
//            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 500;

ll P[N];
map<ll, int> ht;
vector<string> longs;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(s.size() > W)
        longs.push_back(s);
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      s += "#";
      for(int i = 1; i < s.size(); i ++){
        int j = pf[i - 1];
        while(j > 0 && s[j] != s[i])
          j = pf[j - 1];
        if(s[j] == s[i])
          ++ j;
        pf[i] = j;
      }
      int res = 0;
      for(int i = 0; i < longs.size(); i ++){
        for(int k = s.size(); k < longs[i].size() + s.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && s[j] != longs[i][k - s.size()])
            j = pf[j - 1];
          if(s[j] == longs[i][k - s.size()])
            ++ j;
          pf[k] = j;
          if(pf[k] == s.size())
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 500;

ll P[N];
map<ll, int> ht;
vector<string> longs;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(s.size() > W)
        longs.push_back(s);
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      s += "#";
      for(int i = 1; i < s.size(); i ++){
        int j = pf[i - 1];
        while(j > 0 && s[j] != s[i])
          j = pf[j - 1];
        if(s[j] == s[i])
          ++ j;
        pf[i] = j;
      }
      int res = 0;
      for(int i = 0; i < longs.size(); i ++){
        for(int k = s.size(); k < longs[i].size() + s.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && s[j] != longs[i][k - s.size()])
            j = pf[j - 1];
          if(s[j] == longs[i][k - s.size()])
            ++ j;
          pf[k] = j;
          if(pf[k] == s.size())
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20000;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= min((int)s.size(), W); lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = (int)all[i].second.size() - 1;
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[sz + k];
          while(j > 0 && f[j] != s[k])
            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k + sz + 1] = j;
          if(pf[k + sz + 1] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[m] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
//        for(int k = 0; k < s.size(); k ++){
//          int j = pf[f.size()];
//          while(j > 0 && f[j] != s[k])
//            j = pf[j - 1];
//          if(f[j] == s[k])
//            ++ j;
//          pf[k] = j;
//          if(pf[k] >= sz)
//            res ++;
//        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 3e5 + 200;
const int W = 300;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        int j = 0;
        for(int k = 1; k <= m; k ++){
          while(j > 0 && longs[i][j] != longs[i][k])
            j = p[j - 1];
          if(longs[i][j] == longs[i][k])
            ++ j;
          p[k] = j;
        }
        j = 0;
        for(int k = 0; k < n; k ++){
          while(j > 0 && s[k] != longs[i][j])
            j = p[j - 1];
          if(s[k] == longs[i][j])
            ++ j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 500;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[m] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 3e5 + 200;
const int W = 500;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        for(int k = 1; k <= m; k ++){
          int j = p[k - 1];
          while(j > 0 && longs[i][k] != longs[i][j])
            j = p[j - 1];
          if(longs[i][k] == longs[i][j])
            ++ j;
          p[k] = j;
        }
        for(int k = 0; k < n; k ++){
          int j = p[m + k];
          while(j > 0 && longs[i][j] != s[k])
            j = p[j - 1];
          if(longs[i][j] == s[k])
            ++ j;
          p[m + k] = j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[m] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz && k >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const int N = 3e5 + 200;
const int W = 500;

int go[N][26], p[N], g, cnt = 1, value[N], cost[N];
string longs[N];

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int q; cin >> q;
  while(q --){
    int t;
    string s;
    cin >> t >> s;
    int n = s.size();
    if(t == 1 || t == 2){
      if(n <= W){
        int a = 1;
        for(int i = 0; i < n; i ++){
          int c = s[i] - 'a';
          if(go[a][c] == 0)
            go[a][c] = ++cnt;
          a = go[a][c];
        }
        value[a] += (3 - 2 * t);
      }else{
        longs[g] = s + "#";
        cost[g] = 3 - 2 * t;
        g ++;
      }
    }else{
      ll ans = 0;
      for(int i = 0; i < n; i ++){
        int a = 1;
        for(int j = i; j < n; j ++){
          int c = s[j] - 'a';
          if(go[a][c] == 0)
            break;
          a = go[a][c];
          ans += value[a];
        }
      }
      for(int i = 0; i < g; i ++){
        int m = longs[i].size(); m --;
        if(m > n)
          continue;
        for(int k = 1; k <= m; k ++){
          int j = p[k - 1];
          while(j > 0 && longs[i][k] != longs[i][j])
            j = p[j - 1];
          if(longs[i][k] == longs[i][j])
            ++ j;
          p[k] = j;
        }
        for(int k = 0; k < n; k ++){
          int j = p[m + k];
          while(j > 0 && longs[i][j] != s[k])
            j = p[j - 1];
          if(longs[i][j] == s[k])
            ++ j;
          p[m + k] = j;
          if(j == m)
            ans += cost[i];
        }
      }
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 500;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[f.size()];
          while(j > 0 && f[j] != s[k])
            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k] = j;
          if(pf[k] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 20;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[m] = true;
      if(t == 1)
        all.push_back({m, s});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = all[i].second.size();
        string f = all[i].second + "#" + s;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
          if(pf[k] == n)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
const ll p = 31;

const int N = 4e5 + 200;
const int W = 500;

ll P[N];
bool ban[N];
map<ll, int> ht, idx;
vector<pair<int, string>> all;
int pf[N];

ll gethash(const string& s){
  ll h = 0;
  for(int j = 0; j < s.size(); j ++){
    h = (h * p + (s[j] - 'a' + 1)) % mod;
  }
  return h;
}

int main(){
#ifdef LOCAL
//  freopen("test.txt", "r", stdin);
#endif
//  ios_base::sync_with_stdio(0);
//  cin.tie(0); cout.tie(0);
  P[0] = 1;
  for(int i = 1; i < N; i ++)
    P[i] = P[i - 1] * p % mod;
  int m; cin >> m;
  while(m --){
    int t;
    string s;
    cin >> t >> s;
    if(t == 1 || t == 2){
      ht[gethash(s)] += (t == 1 ? 1 : -1);
      if(t == 1)
        idx[gethash(s)] = m;
      else
        ban[idx[gethash(s)]] = true;
      if(t == 1)
        all.push_back({m, s + "#"});
    }else if(s.size() <= W){
      int res = 0;
      for(int lng = 1; lng <= W; lng ++){
        ll h = 0;
        for(int j = 0; j < s.size(); j ++){
          h = (h * p + (s[j] - 'a' + 1)) % mod;
          if(j >= lng){
            h -= (s[j - lng] - 'a' + 1) * P[lng] % mod;
            h = (h + mod) % mod;
          }
          if(j + 1 >= lng){
            res += ht[h];
          }
        }
      }
      cout << res << endl;
    }else{
      int res = 0;
      for(int i = 0; i < all.size(); i ++){
        if(ban[all[i].first]) continue;
        int sz = (int)all[i].second.size() - 1;
        string& f = all[i].second;
        for(int k = 1; k < f.size(); k ++){
          int j = pf[k - 1];
          while(j > 0 && f[j] != f[k])
            j = pf[j - 1];
          if(f[j] == f[k])
            ++ j;
          pf[k] = j;
        }
        for(int k = 0; k < s.size(); k ++){
          int j = pf[sz + k];
          while(j > 0 && f[j] != s[k])
            j = pf[j - 1];
          if(f[j] == s[k])
            ++ j;
          pf[k + sz + 1] = j;
          if(pf[k + sz + 1] >= sz)
            res ++;
        }
      }
      cout << res << endl;
    }
  }
}