#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve(){
    int N;
    cin >> N;
    int K;
    cin >> K;

    map<int,int> screen;
    int count = 0;
    int time = 0;
    int maxm = -1;
    REP(i,N) {
       int a;cin >> a;
       ++time;
       if(count < K) {
           if(screen[a]!=0)  continue;
           else
           {
              screen[a] = time;
              maxm = a;
              ++count;
           }

       }else
       {
          if(screen[a]!=0)   continue;
          else
          {
             screen[maxm] = 0;
             screen[a] = time;
             maxm = a;
          }
       }
    }

    vector<pint> ret;
    int cc = 0;
    for(auto i : screen) {
        if(cc < K && i.second) {
             ret.push_back({i.first,i.second});
             ++cc;
        }else
        {
           break;
        }
    }

    sort(ret.begin(),ret.end(),[&](pint x,pint y) {return x.second > y.second;});
    cout << ret.size() << endl;
    for(pint i: ret) {
        cout << i.first << " ";
    }cout << endl;
}

int main(){

 solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve(){
    int N;
    cin >> N;
    int K;
    cin >> K;

    map<int,int> screen;
    int count = 0;
    int time = 0;
    int maxm = -1;
    REP(i,N) {
       int a;cin >> a;
       ++time;
       if(count < K) {
           if(screen.count(a))  continue;
           else
           {
              screen[a] = time;
              maxm = a;
           }

       }else
       {
          if(screen.count(a))   continue;
          else
          {
             screen[maxm] = 0;
             maxm = a;
             screen[a] = time;
          }
       }
    }

    vector<pint> ret;
    int cc = 0;
    for(auto i : screen) {
        if(cc < K && i.second) {
             ret.push_back({i.first,i.second});
             ++cc;
        }else
        {
           break;
        }
    }

    sort(ret.begin(),ret.end(),[&](pint x,pint y) {return x.second > y.second;});
    cout << ret.size() << endl;
    for(pint i: ret) {
        cout << i.first << " ";
    }cout << endl;
}

int main(){

 solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<int> A(N);
  cin >> A;
  vector<int> ret;

   map<int,int> screen;

   int firstPos = -1;
   int cc = 0;
  for(int i = 0;i < N; ++i) {
     int curr = A[i];

     if(screen[curr] != 0)   continue;

     if(firstPos == -1){
        firstPos = 0;
     }
     if(cc < K) {
        ret.push_back(curr);
        screen[curr] = i + 1;
        ++cc;
     }else
     {
        screen[A[firstPos]] = 0;
        ++firstPos;
        ret.push_back(curr);
        screen[curr] = i + 1;
     }

  }

   cout << ret.size() - 1 - firstPos + 1 << endl;
  for(int i = ret.size() - 1;i >= firstPos; --i) {
     cout << ret[i] << " ";
  }cout << endl;
}

int main(){

 solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve() {
  int N; cin >> N;
  int K;cin >> K;
  vector<int> A(N);
  cin >> A;
  vector<int> ret;

  map<int,int> screen;

   int firstPos = 0;
   int cc = 0;
  for(int i = 0;i < N; ++i) {
     int curr = A[i];
     if(screen[curr] != 0)   continue;

     if(cc < K) {
        ret.push_back(curr);
        screen[curr] = i + 1;
        ++cc;
     }else
     {
        screen[ret[firstPos]] = 0;
        ++firstPos;
        ret.push_back(curr);
        screen[curr] = i + 1;
     }
  }

  cout << ret.size() - 1 - firstPos + 1 << endl;
  for(int i = ret.size() - 1;i >= firstPos; --i) {
     cout << ret[i] << " ";
  }

}

int main(){

 solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve() {
   int N;
   cin >> N;
   int K;
   cin >> K;

   int t = 0;
   int cc = 0;

   map<int,int> screen;
   int prev = -1;
   for(int i = 0;i < N; ++i) {
      ++t;
      int a;
      cin >> a;

      if(screen[a] != 0) {
         continue;
      }

      if(cc < K) {
         ++cc;
         screen[a] = t;
         prev = a;
      }else
      {
         cc = K;
         screen[prev] = 0;
         screen[a] = t;
         prev = a;
      }

   }

   vector<pint> ret;

   for(auto i : screen) {
      if(i.second) {
         ret.push_back({i.first,i.second});
      }
   }

   sort(ALL(ret),[&](pint x,pint y) { return x.second > y.second;});

   cout << ret.size() << endl;
   for(auto i : ret) {
      cout << i.first << " ";
   }cout << endl;
}

int main(){

 solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
using lint  =                           long long int;
using pint  =                           pair<int, int>;
using plint =                           pair<lint, lint>;
#define vli                             vector<lint>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             (1LL<63)-1
#define MOD                             1000000007
#define SZ(x)                           ((lint)(x).size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 REP(i, n) FOR(i,0,n)
#define                                 IREP(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define                                 debug(x) cerr << #x << " " << (x) << endl
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

lint pow_mod(lint base,lint expo) {
   lint ret = 1;
   for(;expo;) {
      if(expo&1)  ret = (ret*base)%MOD;

      expo = (expo>>1);
      base = (base*base)%MOD;
   }
   return ret;
}

/* -------
         |
    **   |
   -------
   |
   ---------------------------------------------------------------------------------------------------------------------------------*/

void solve(){
    int N;
    cin >> N;
    int K;
    cin >> K;

    map<int,int> screen;
    int count = 0;
    int time = 0;
    int maxm = -1;
    REP(i,N) {
       int a;cin >> a;
       ++time;
       if(count < K) {
           if(screen.count(a))  continue;
           else
           {
              screen[a] = time;
              maxm = a;
           }

       }else
       {
          if(screen.count(a))   continue;
          else
          {
             screen[maxm] = 0;
             maxm = a;
             screen[a] = time;
          }
       }
    }

    vector<pint> ret;
    cout << ret.size() << endl;
    int cc = 0;
    for(auto i : screen) {
        if(cc < K && i.second) {
             ret.push_back({i.first,i.second});
             ++cc;
        }else
        {
           break;
        }
    }

    sort(ret.begin(),ret.end(),[&](pint x,pint y) {return x.second > y.second;});

    for(pint i: ret) {
        cout << i.first << " ";
    }cout << endl;
}

int main(){

 solve();

 return 0;
}


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

int main(){
int n=10,k=4;
cin>>n>>k;
vector<int>A;
for(int i=0;i<n;i++){
    int no;
    cin>>no;
    auto it=find(A.begin(),A.end(),no);
    if(it==A.end()){
        if(A.size()==k) A.erase(A.begin());
        A.push_back(no);
    }
}
string ans;
int i;
        for( i=A.size()-1;i>0;i--) ans+=to_string(A[i])+" ";
ans+=to_string(A[i]);
    cout<<ans<<endl;

return 0;
}


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

int main(){
int n=10,k=4;
cin>>n>>k;
vector<int>A;
for(int i=0;i<n;i++){
    int no;
    cin>>no;
    auto it=find(A.begin(),A.end(),no);
    if(it==A.end()){
        if(A.size()==k) A.erase(A.begin());
        A.push_back(no);
    }
}
string ans;
int i;
cout<<A.size()<<endl;
        for( i=A.size()-1;i>0;i--) ans+=to_string(A[i])+" ";
ans+=to_string(A[i]);
    cout<<ans<<endl;

return 0;
}


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

int main(){
int n=10,k=4;
cin>>n>>k;
vector<int>A;
for(int i=0;i<n;i++){
    int no;
    cin>>no;
    auto it=find(A.begin(),A.end(),no);
    if(it==A.end()){
        if(A.size()==k) A.erase(A.begin());
        A.push_back(no);
    }
}
string ans;
        for(int i=A.size()-1;i>=0;i--) ans+=to_string(A[i])+" ";
    cout<<ans;

return 0;
}


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

int main(){
int n=10,k=4;
cin>>n>>k;
vector<int>A;
for(int i=0;i<n;i++){
    int no;
    cin>>no;
    auto it=find(A.begin(),A.end(),no);
    if(it==A.end()){
        if(A.size()==k) A.erase(A.begin());
        A.push_back(no);
    }
}
string ans;
int i
        for( i=A.size()-1;i>0;i--) ans+=to_string(A[i])+" ";
ans+=to_string(A[i]);
    cout<<ans;

return 0;
}


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

#define mx 201
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  string s="";
  bool vis[mx]={0};
  f(i,n){
      cin>>a[i];

  }

  f(i,n){
     if(vis[a[i]]!=1){
         char c=a[i]+'0';
         s= c + s;
         vis[a[i]]=1;
         //spt(s);
     }
     if(s.size()>k){
         vis[s[s.size()-1]-'0']=0;
         s.erase(s.size()-1,1);
     }
  }
  //en;
  pt(s.size());
  f(i,s.size()){
      spt(s[i]);
  }
  en;

}


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

#define mx 1000000000
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  list<int> l;
  map<int,int> mp;
  f(i,n){
      cin>>a[i];
      mp[a[i]]=0;
  }

  f(i,n){
     if(mp[a[i]]!=1){
         l.push_front(a[i]);
        mp[a[i]]=1;
         //spt(s);
     }
     if(l.size()>k){
         auto it=l.end();it--;
         mp[*it]=0;
         l.pop_back();
     }
  }
  //en;
  pt(l.size());
  for(auto it=l.begin();it!=l.end();it++){
      spt(*it);
  }
  en;

}


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

#define mx 1000000000
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  string s="";
  bool vis[mx]={0};
  f(i,n){
      cin>>a[i];

  }

  f(i,n){
     if(vis[a[i]]!=1){
         char c=a[i]+'0';
         s= c + s;
         vis[a[i]]=1;
         //spt(s);
     }
     if(s.size()>k){
         vis[s[s.size()-1]-'0']=0;
         s.erase(s.size()-1,1);
     }
  }
  //en;
  pt(s.size());
  f(i,s.size()){
      spt(s[i]);
  }
  en;

}


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

#define mx 201
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  string s="";
  bool vis[mx]={0};
  f(i,n){
      cin>>a[i];

  }

  f(i,n){
     if(vis[a[i]]!=1){
         char c=a[i]+'0';
         s= c + s;
         vis[a[i]]=1;
         //spt(s);
     }
     if(s.size()>k){
         vis[s[s.size()-1]-'0']=0;
         s.erase(s.size()-1,1);
     }
  }
  //en;
  f(i,s.size()){
      spt(s[i]);
  }
  en;

}


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

#define mx 1000000000
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  string s="";
  map<int,int> mp;
  f(i,n){
      cin>>a[i];
      mp[a[i]]=0;
  }

  f(i,n){
     if(mp[a[i]]!=1){
         char c=a[i]+'0';
         s= c + s;
        mp[a[i]]=1;
         //spt(s);
     }
     if(s.size()>k){
         mp[s[s.size()-1]-'0']=0;
         s.erase(s.size()-1,1);
     }
  }
  //en;
  pt(s.size());
  f(i,s.size()){
      spt(s[i]);
  }
  en;

}


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

#define mx 201
#define mod 1000000007
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)

#define r(i,n) for(int i=n-1;i>=0;i--)
#define fv(a,b,c) for(ll a=b;a<=c;a++)
#define rv(a,b,c) for(int a=b;a>c;a--)
#define re return
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define re return
#define fir first
#define sec second
#define ll long long int
#define str string
#define yes  cout<<"YES"
#define no    cout<<"NO"
#define pt(a)    cout<<a<<endl
#define spt(a)  cout<<a<<" "
#define sf(a)    cin>>a
#define en     cout<<endl

// int dp[51]={0};
// bool vis[1000]={0};
// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
//     return(a.second<b.second);
// }

// ll myXOR(ll x, ll y)
// {
//   return (x | y) & (~x | ~y);
// }

// void primeFactors(int n,map<int,int> &mp)
// {
//     for (int i=1;i<=n;i++)
//         if (n%i==0)
//             mp[i]++;
// }
// bool isPrime(int n)
// {

//     if (n <= 1)
//         return false;

//     for (int i = 2; i < n; i++)
//         if (n % i == 0)
//             return false;

//     return true;
//}
int main(){
  int n,k,t=0;
  cin>>n>>k;
  int a[n];
  string s="";
  bool vis[mx]={0};
  f(i,n){
      cin>>a[i];

  }

  f(i,n){
     if(vis[a[i]]!=1){
         char c=a[i]+'0';
         s= c + s;
         vis[a[i]]=1;
         spt(s);
     }
     if(s.size()>k){
         vis[s[s.size()-1]-'0']=0;
         s.erase(s.size()-1,1);
     }
  }
  en;
  f(i,s.size()){
      spt(s[i]);
  }
  en;

}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
   set<int>s;
vector<int>v;
   rep(i,0,n)
   {
     cin>>a[i];
   }
  s.insert(a[0]);
  v.pb(a[0]);//cout<<v.size()<<"iiii"<<a[0]<<endl;
  set<int>::iterator it;
  rep(i,1,n)
  {

     int d=s.size();int count=0;int flag=0;
      for(it=s.begin();it!=s.end();it++)
      {
          if(*it==a[i]){flag=1;break;}
      }

      if(flag==0)v.pb(a[i]);
      s.insert(a[i]);
  }
  if(v.size()<k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";   }
  else { cout<<k<<endl; for(int i=k-1;i>=0;i--)cout<<v[i]<<" ";  }

return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
  map<int,int>m;
  vector<int>v;

   rep(i,0,n)
   {
     cin>>a[i];
   }
  m[a[0]]++;
  v.pb(a[0]);
  rep(i,1,n)
  {
    int flag=0;
    if(k<v.size()) for(int j=v.size()-1;j>=v.size()-k;j--){if(v[j]==a[i]){flag++;  break;}}
    else for(int j=v.size()-1;j>=0;j--){if(v[j]==a[i]){flag++; break;}}
    if(flag==0){v.pb(a[i]);}
  }
//
//cout<<v.size()<<" j "<<endl;
//rep(i,0,v.size())cout<<v[i]<<" ";
//cout<<endl;

  if(v.size()<k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";   }
  else { cout<<k<<endl; for(int i=v.size()-1;i>=v.size()-k;i--)cout<<v[i]<<" ";  }

return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
  map<int,int>m;
  vector<int>v;
   rep(i,0,n)
   {
     cin>>a[i];
   }
  m[a[0]]++;
  v.pb(a[0]);
  rep(i,1,n)
  {
      if(m[a[i]]==0){v.pb(a[i]);m[a[i]]++;}
      else continue;
  }
  if(v.size()<k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";   }
  else { cout<<k<<endl; for(int i=v.size()-1;i>=v.size()-k;i--)cout<<v[i]<<" ";  }

return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
  map<int,int>m;
  vector<int>v;

   rep(i,0,n)
   {
     cin>>a[i];
   }
  m[a[0]]++;
  v.pb(a[0]);
  rep(i,1,n)
  {
    int flag=0;
    if(k<v.size()){ for(int j=v.size()-1;j>=v.size()-k;j--){if(v[j]==a[i]){flag++;  break;}}}
    else {for(int j=v.size()-1;j>=0;j--){if(v[j]==a[i]){flag++; break;}}}
    if(flag==0){v.pb(a[i]);}
   // rep(l,0,v.size())cout<<v[l]<<" ";cout<<endl;
  }
//
//cout<<v.size()<<" j "<<endl;
//rep(i,0,v.size())cout<<v[i]<<" ";
//cout<<endl;

  if(v.size()<=k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";  return 0; }
  else { cout<<k<<endl; for(int i=v.size()-1;i>=v.size()-k;i--)cout<<v[i]<<" "; return 0;  }

return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
  map<int,int>m;
  vector<int>v;
   rep(i,0,n)
   {
     cin>>a[i];
   }
  m[a[0]]++;
  v.pb(a[0]);
  rep(i,1,n)
  {
      if(m[a[i]]==0){v.pb(a[i]);m[a[i]]++;}
      else continue;
  }
  if(v.size()<k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";   }
  else { cout<<k<<endl; for(int i=k-1;i>=0;i--)cout<<v[i]<<" ";  }

return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define ll long long
using namespace std;

int main()
{
   int n,k; cin>>n>>k;
   int a[n];
  map<int,int>m;
  vector<int>v;

   rep(i,0,n)
   {
     cin>>a[i];
   }
  m[a[0]]++;
  v.pb(a[0]);
  rep(i,1,n)
  {
    int flag=0;
    if(k<=v.size()){ for(int j=v.size()-1;j>=v.size()-k;j--){if(v[j]==a[i]){flag++;  break;}}}
    else {for(int j=v.size()-1;j>=0;j--){if(v[j]==a[i]){flag++; break;}}}
    if(flag==0){v.pb(a[i]);}
  }
//
//cout<<v.size()<<" j "<<endl;
//rep(i,0,v.size())cout<<v[i]<<" ";
//cout<<endl;
//
//cout<<v.size()<<"  jjj   "<<endl;
//rep(i,0,v.size())cout<<v[i]<<" ";
//cout<<endl;
  if(v.size()<=k){cout<<v.size()<<endl; for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<" ";  return 0; }
  else { cout<<k<<endl; for(int i=v.size()-1;i>=v.size()-k;i--)cout<<v[i]<<" "; return 0;  }

return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	long int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++ ){
		if(v.size()>=k){
			mp[v[0]]=0;
			v.erase(v.begin());
		}
		if(mp.find(a[i])==mp.end()){
			v.push_back(a[i]);
		}
	}
	for(int i = v.size()-1 ; i >= 0 ; i-- ){
		cout<<v[i]<<' ';
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	map<int,int>mp;
	vector<int>v;
	for(int i = 0 ; i < n ; i++ ){
		if(v.size()==k){
			v.pop_back();
		}
		if(find(v.begin(), v.end(), a[i])==v.end())
		v.push_back(a[i]);
	}
	cout<<v.size()<<'\n';
	for(int i = v.size() -1 ; i>=0 ; i--){
		cout<<v[i]<<' ';
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	for(int i = 0 ; i < n ; i++ ){
		if(v.size()>k){
			v.pop_back();
		}
		if(find(v.begin(), v.end(), a[i])==v.end())
		{
			v.push_back(a[i]);
		}
	}
	cout<<v.size()<<'\n';
	for(int i = v.size() -1 ; i>=0 ; i--){
		cout<<v[i]<<' ';
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	long int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++ ){
		if(mp[a[i]]==1){
			continue;
		}
		else{
			if(v.size()>=k){
				mp[v[0]]=0;
				v.erase(v.begin());
			}
			else{
				v.push_back(a[i]);
				mp[a[i]]=1;
			}
		}
	}
	cout<<v.size()<<'\n';
	for(int i = v.size()-1 ; i >= 0 ; i-- ){
		cout<<v[i]<<' ';
	}
}

/*7 2
1 2 3 2 1 3 2

1
1 2
2 3
3 1
1 2
*/


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	long int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	stack<long int>s;
	map<long int,int>mp;
	for(int i = 0 ; i < n ; i++){
		if(s.size()>=k){
			mp[s.top()]=0;
			s.pop();
		}
		if(mp[a[i]]==0){
			s.push(a[i]);
			mp[a[i]]=1;
		}
	}
	cout<<s.size()<<'\n';
	while(!s.empty()){
		cout<<s.top()<<' ';
		s.pop();
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	long int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++ ){
		if(v.size()>=k){
			mp[v[0]]=0;
			v.erase(v.begin());
		}
		if(mp.find(a[i])==mp.end()){
			v.push_back(a[i]);
		}
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	stack<int>s;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++){
		if(s.size()>k){
			mp[s.top()]=0;
			s.pop();
		}
		if(mp[a[i]]==0){
			s.push(a[i]);
			mp[a[i]]=1;
		}
	}
	cout<<s.size()<<'\n';
	while(!s.empty()){
		cout<<s.top()<<' ';
		s.pop();
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	stack<int>s;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++){
		if(s.size()>=k){
			mp[s.top()]=0;
			s.pop() ;
		}
		if(mp[a[i]]==0){
			s.push(a[i]);
			mp[a[i]]=1;
		}
	}
	cout<<s.size()<<'\n';
	while(!s.empty()){
		cout<<s.top()<<' ';
		s.pop();
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	stack<int>s;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++){
		if(s.size()>=k){
			mp[s.top()]=0;
			s.pop();
		}
		if(mp[a[i]]==0){
			s.push(a[i]);
			mp[a[i]]=1;
		}
	}
	cout<<s.size()<<'\n';
	while(!s.empty()){
		cout<<s.top()<<' ';
		s.pop();
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	long int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	map<int,int>mp;
	for(int i = 0 ; i < n ; i++ ){
		if(mp[a[i]]==1){
			continue;
		}
		else{
			if(v.size()>=k){
				mp[v[0]]=0;
				v.erase(v.begin());
			}
				v.push_back(a[i]);
				mp[a[i]]=1;
		}
	}
	cout<<v.size()<<'\n';
	for(int i = v.size()-1 ; i >= 0 ; i-- ){
		cout<<v[i]<<' ';
	}
}

/*7 2
1 2 3 2 1 3 2

1
1 2
2 3
3 1
1 2
*/


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin >> n >>k;
	int a[n];
	for(int i = 0 ; i < n ;i++ ){
		cin >> a[i];
	}
	vector<int>v;
	for(int i = 0 ; i < n ; i++ ){
		if(v.size()>k){
			v.pop_back();
		}
		if(find(v.begin(), v.end(), a[i])==v.end())
		{
			v.push_back(a[i]);
		}
	}
	cout<<v.size()<<'\n';
	for(int i = v.size() -1 ; i>=0 ; i--){
		cout<<v[i]<<' ' ;
	}
}


#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    set<lli> q;
    vector<lli> vec;
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=vec.size()-1;i>=vec.size()-k;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }
    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    set<lli> q;
    vector<lli> vec;
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=k-1;i>=0;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    set<lli> q;
    vector<lli> vec;
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=vec.size()-1;i>=vec.size()-k;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }
    cout<<endl;
    return 0;
}


/*#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    unordered_set<lli> q;
    vector<lli> vec;
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";
    for(int i=vec.size()-1;i>=vec.size()-k;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }
    return 0;
}
*/
/*

#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    set<lli> q;
    vector<lli> vec;
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";
    for(int i=k-1;i>=0;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }

    return 0;
}*/

#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    vector<lli> v;
    vector<lli> r;

    forz(i,n){
        lli x;
        cin>>x;

        if(find(v.begin(),v.end(),x)==v.end())
            {v.push_back(x);
            }

    }
    lli m;
    m=v.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=v.size()-1;i>=v.size()-k;i--){
        if(i<0) break;
        cout<<v[i]<<" ";

    }
    //cout<<endl;
    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    int n, k;
    cin>>n>>k;
    unordered_set<int> q;
    vector<int> vec;
    forz(i,n){
        int x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            vec.push_back(x);}

    }
    int m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=vec.size()-1;i>=vec.size()-k;i--){
        if(i<0) break;
        cout<<vec[i]<<" ";

    }
    return 0;
}


#include<bits/stdc++.h>

#define forz(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define lli long long int

using namespace std;

int main(){
    int n,k;
    cin>>n>>k;
    vector<int> id;
    forz(i,n){
        int idn;
        cin>>idn;
        if(find(id.begin(),id.end(),idn)==id.end()){
            if(id.size()>=k) id.pop_back();
            id.insert(id.begin(),idn);
        }
    }
    cout<<id.size()<<endl;

    for(auto t=id.begin();t!=id.end();t++)
        cout<<*t<<" ";
    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define forz(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
    lli n, k;
    cin>>n>>k;
    set<lli> q;
    int j=0;
    lli arr[n]={0};
    forz(i,n){
        lli x;
        cin>>x;

        if(q.find(x)==q.end())
            {q.insert(x);
            arr[j]=x; j++;}

    }
    lli m;
    m=q.size();
    k=(k>m?m:k);
    cout<<k<<endl;
    /*auto it=q.begin();
    while(k--){
        it++;
    }
    it++;
    auto itr=q.begin();
    for (itr = q.begin(); itr !=it; itr++)
        cout << *itr << " ";*/
    for(int i=j-1;i>=j-k;i--){
        if(i<0) break;
        cout<<arr[i]<<" ";

    }
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
	int n, k;
	cin>>n>>k;
	queue<int>q;
	set<int>a;
	int s[n+1];
	for (int i=1; i<=n; i++){
	    cin>>s[i];
	}
	int p=0;
	for (int i=1; i<=n; i++){
		int x=a.size ();
		a.insert(s[i]);
		if (x==a.size ()){
	      	continue;
		}
			q.push(s[i]);
		if (q.size ()==k){
			p=i; break;
		}
	}
	for (int i=p+1; i<=n; i++){
		int x=a.size ();
		a.insert(s[i]);
		if (x!=a.size ()){
		    a.erase(q.front ());
			q.pop ();
			q.push(s[i]);
		}
	}
    cout<<q.size ()<<endl;
    vector<int>ans;
	while (!q.empty ()){
		ans.push_back(q.front ());
		q.pop ();
	}
	for (int i=ans.size ()-1; i>=0; i--){
		cout<<ans[i]<<' ';
	}
return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	vector<int>cnt;
	if (n==k){
		vector<int>s;
		for (int i=1; i<=n; i++){
			int x=1;
			for (int j=0; j<s.size (); j++){
				if (s[j]==a[i]){
					x=0; break;
				}
			}
			if (x==1){
				s.push_back(a[i]);
			}
		}
		cout<<s.size ()<<endl;
		for (int i=s.size ()-1; i>=0; i--)
		    cout<<s[i]<<' ';
		return 0;
	}
	for (int i=1; i<=k; i++){
		cnt.push_back(a[i]);
	}
	for (int i=k+1; i<=n; i++){
		int x=1;
		for (int j=0; j<k; j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}

		if (x==1){
			for (int p=0; p<k-1; p++){
				cnt[p]=cnt[p+1];
			}
			cnt[k-1]=a[i];
		}
	}
	vector<int>ans;
	for (int i=k-1; i>=0; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (x==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(x);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	vector<int>cnt;
	if (n==k){
		vector<int>s;
		for (int i=1; i<=n; i++){
			int x=1;
			for (int j=0; j<s.size (); j++){
				if (s[j]==a[i]){
					x=0; break;
				}
			}
			if (x==1){
				s.push_back(a[i]);
			}
		}
		cout<<s.size ()<<endl;
		for (int i=s.size ()-1; i>=0; i--)
		    cout<<s[i]<<' ';
		return 0;
	}
	for (int i=1; i<=k; i++){
		cnt.push_back(a[i]);
	}
	for (int i=k+1; i<=n; i++){
		int x=1;
		for (int j=0; j<k; j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}

		if (x==1){
			for (int p=0; p<k-1; p++){
				cnt[0]=cnt[p+1];
			}
			cnt[k-1]=a[i];
		}
	}
	vector<int>ans;
	for (int i=k-1; i>=0; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (cnt[i]==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(cnt[i]);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	int cnt[k+1];
	for (int i=1; i<=k; i++){
		cnt[i]=a[i];
	}
	for (int i=k+1; i<=n; i++){
		int x=1;
		for (int j=1; j<=k; j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}

		if (x==1){
			for (int p=1; p<k; p++){
				cnt[p]=cnt[p+1];
			}
			cnt[k]=a[i];
		}
	}
	vector<int>ans;
	for (int i=k; i>=1; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (cnt[i]==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(cnt[i]);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	int cnt[k+1];
	if (n==k){
		vector<int>s;
		for (int i=1; i<=n; i++){
			int x=1;
			for (int j=0; j<s.size (); j++){
				if (s[j]==a[i]){
					x=0; break;
				}
			}
			if (x==1){
				s.push_back(a[i]);
			}
		}
		cout<<s.size ()<<endl;
		for (int i=s.size ()-1; i>=0; i--)
		    cout<<s[i]<<' ';
		return 0;
	}
	for (int i=1; i<=k; i++){
		cnt[i]=a[i];
	}
	for (int i=k+1; i<=n; i++){
		int x=1;
		for (int j=1; j<=k; j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}

		if (x==1){
			for (int p=1; p<k; p++){
				cnt[p]=cnt[p+1];
			}
			cnt[k]=a[i];
		}
	}
	vector<int>ans;
	for (int i=k; i>=1; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (cnt[i]==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(cnt[i]);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	vector<int>cnt;
	if (n==k){
		vector<int>s;
		for (int i=1; i<=n; i++){
			int x=1;
			for (int j=0; j<s.size (); j++){
				if (s[j]==a[i]){
					x=0; break;
				}
			}
			if (x==1){
				s.push_back(a[i]);
			}
		}
		cout<<s.size ()<<endl;
		for (int i=s.size ()-1; i>=0; i--)
		    cout<<s[i]<<' ';
		return 0;
	}
	for (int i=1; i<=n; i++){
		int x=1;
		for (int j=0; j<cnt.size (); j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}
		if (x==1 && cnt.size ()<k)
		   cnt.push_back (a[i]);

		if (x==1 && cnt.size ()==k){
			for (int p=0; p<k-1; p++){
				cnt[p]=cnt[p+1];
			}
			cnt[k-1]=a[i];
		}
	}
	vector<int>ans;
	for (int i=cnt.size ()-1; i>=0; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (x==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(x);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
    int n, k;
    cin>>n>>k;
    int a[n+1];
    for (int i=1; i<=n; i++){
    	cin>>a[i];
	}
	vector<int>cnt;

	for (int i=1; i<=k; i++){
		cnt.push_back(a[i]);
	}
	for (int i=k+1; i<=n; i++){
		int x=1;
		for (int j=0; j<k; j++){
			if (cnt[j]==a[i]){
			x=0;	break;
			}
		}

		if (x==1){
			for (int p=0; p<k-1; p++){
				cnt[p]=cnt[p+1];
			}
			cnt[k-1]=a[i];
		}
	}
	vector<int>ans;
	for (int i=k-1; i>=0; i--){
		int x=cnt[i];
		int y=1;
		for (int j=0; j<ans.size (); j++){
			if (x==ans[j]){
				y=0; break;
			}
		}
		if (y==1)
		   ans.push_back(x);
	}
	cout<<ans.size ()<<endl;
	for (int i=0; i<ans.size (); i++)
	    cout<<ans[i]<<' ';

return 0;
}


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

int main() {
    int n,k,a;
    cin>>n>>k;

    list<int> lst;
    map<int, bool> vis;
    for(int i=0; i<n; i++) {
        cin>>a;
        if(lst.size() < k) {
            if(vis.find(a) == vis.end()) {
                lst.push_front(a);
                vis[a] = true;
            }
            continue;
        }

        if(vis.find(a)==vis.end() || vis[a]==false) {
            vis[a] = true;
            lst.pop_back();
            lst.push_front(a);
        }
    }

    cout<<lst.size()<<endl;
    for(auto it=lst.begin(); it!=lst.end(); it++)
        cout<<(*it)<<" ";
    cout<<endl;

    return 0;
}


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

int main() {
    int n,k,a;
    cin>>n>>k;

    list<int> lst;
    map<int, bool> vis;
    for(int i=0; i<n; i++) {
        cin>>a;
        if(lst.size() < k) {
            if(vis.find(a) == vis.end()) {
                lst.push_front(a);
                vis[a] = true;
            }
            continue;
        }

        if(vis.find(a)==vis.end() || vis[a]==false) {
            vis[a] = true;
            vis[lst.back()] = false;
            lst.pop_back();
            lst.push_front(a);
        }
    }

    cout<<lst.size()<<endl;
    for(auto it=lst.begin(); it!=lst.end(); it++)
        cout<<(*it)<<" ";
    cout<<endl;

    return 0;
}


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

int main() {
    int n,k,a;
    cin>>n>>k;

    list<int> lst;
    map<int, bool> vis;
    for(int i=0; i<n; i++) {
        cin>>a;
        if(lst.size() < k && vis.find(a) == vis.end()) {
            lst.push_front(a);
            vis[a] = true;
            continue;
        }

        if(vis.find(a)==vis.end() || vis[a]==false) {
            vis[a] = true;
            lst.pop_back();
            lst.push_front(a);
        }
    }

    cout<<lst.size()<<endl;
    for(auto it=lst.begin(); it!=lst.end(); it++)
        cout<<(*it)<<" ";
    cout<<endl;

    return 0;
}


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

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

    list<int> lst;
    map<int, bool> vis;
    for(int i=0; i<n; i++) {
        cin>>a;
        if(lst.size() < k && vis.find(a) == vis.end()) {
            lst.push_front(a);
            vis[a] = true;
            contniue;
        }

        if(vis.find(a)==vis.end() || vis[a]==false) {
            vis[a] = true;
            lst.pop_back();
            lst.push_front(a);
        }
    }

    cout<<lst.size()<<endl;
    for(int i=0; i<lst.size(); i++)
        cout<<lst[i]<<" ";
    cout<<endl;

    return 0;
}


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

int main() {
    int n,k,a;
    cin>>n>>k;

    list<int> lst;
    map<int, bool> vis;
    for(int i=0; i<n; i++) {
        cin>>a;
        if(lst.size() < k) {
            if(vis.find(a) == vis.end()) {
                lst.push_front(a);
                vis[a] = true;
                continue;
            }
        }

        if(vis.find(a)==vis.end() || vis[a]==false) {
            vis[a] = true;
            lst.pop_back();
            lst.push_front(a);
        }
    }

    cout<<lst.size()<<endl;
    for(auto it=lst.begin(); it!=lst.end(); it++)
        cout<<(*it)<<" ";
    cout<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,k,j=0,min=0,sum=0,t;
    cin>>n>>k;
    vector<long long int>a(n);
    vector<long long int>s;
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    {
        if(find(s.begin(),s.end(),a[i])==s.end())
        s.push_back(a[i]);
        if(s.size()==k+1)
        s.erase(s.end()-1);
    }
    cout<<s.size()<<endl;
    for(i=s.size()-1;i>=0;i--)
    cout<<s[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,k,j=0,min=0,sum=0,t;
    cin>>n>>k;
    vector<long long int>a(n);
    vector<long long int>s(0);
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    {
        if(find(s.begin(),s.end(),a[i])==s.end())
        s.push_back(a[i]);
        if(s.size()==k+1)
        s.erase(s.end()-1);
    }
    cout<<s.size()<<endl;
    for(i=s.size()-1;i>=0;i--)
    cout<<s[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,k,j=0,min=0,sum=0,t;
    cin>>n>>k;
    vector<long long int>a(n);
    vector<long long int>s;
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    {
        if(find(s.begin(),s.end(),a[i])==s.end())
        s.push_back(a[i]);
        if(s.size()==k+1)
        s.erase(s.begin());
    }
    cout<<s.size()<<endl;
    for(i=s.size()-1;i>=0;i--)
    cout<<s[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,k,j=0,min=0,sum=0,t;
    cin>>n>>k;
    vector<int>a(n);
    vector<int>s;
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    {
        if(find(s.begin(),s.end(),a[i])==s.end())
        s.push_back(a[i]);
        if(s.size()==k+1)
        s.erase(s.end()-1);
    }
    cout<<s.size()<<endl;
    for(i=s.size()-1;i>=0;i--)
    cout<<s[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll id[n];
    rep(i,n)
    cin>>id[i];
    map<ll,ll>position;
    map<ll,ll>m;
    int pos=1,index=1;
    for(int i=0;i<n;i++)
    {
        if(m.find(id[i])==m.end()){
            m[id[i]]=pos;
            position[pos]=id[i];
            pos++;
        }
        if(position.size()>k||m.size()>k)
        {
            ll temp=m[position[index]];
            m.erase(temp);
            position.erase(index);
            index++;
        }
    }
    cout<<position.size()<<endl;
    for(int i=pos-1;i>=index;i--)
        cout<<position[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll x;
    queue<ll>q;
    set<ll>s;
    rep(i,n){
        cin>>x;
        if(s.find(x)==s.end()){
            if(q.size()>=k)
            {
                ll temp=q.front();
                q.pop();
                s.erase(temp);
            }
            s.insert(x);
            q.push(x);
        }
    }
    cout<<q.size()<<endl;
    vector<ll>res;
    while(!q.empty())
    {
        res.push_back(q.front());
        q.pop();
    }
    reverse(res.begin(),res.end());
    for(auto x:res)
        cout<<x<<" ";

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll id[n];
    rep(i,n)
    cin>>id[i];
    map<int,int>position;
    map<int,int>m;
    int pos=1,index=1;
    for(int i=0;i<n;i++)
    {
        if(m.find(id[i])==m.end()){
            m[id[i]]=pos;
            position[pos]=id[i];
            pos++;
        }
        if(position.size()>k)
        {
            int temp=m[position[index]];
            m.erase(temp);
            position.erase(index);
            index++;
        }
    }
    cout<<m.size()<<endl;
    for(int i=pos-1;i>=index;i--)
        cout<<position[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll id[n];
    rep(i,n)
    cin>>id[i];
    map<ll,ll>position;
    map<ll,ll>m;
    int pos=1,index=1;
    for(int i=0;i<n;i++)
    {
        if(m.find(id[i])==m.end()){
            m[id[i]]=pos;
            position[pos]=id[i];
            pos++;
        }
        if(position.size()>k)
        {
            int temp=m[position[index]];
            m.erase(temp);
            position.erase(index);
            index++;
        }
    }
    cout<<position.size()<<endl;
    for(int i=pos-1;i>=index;i--)
        cout<<position[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll id[n];
    rep(i,n)
    cin>>id[i];
    map<ll,ll>position;
    map<ll,ll>m;
    int pos=1,index=1;
    for(int i=0;i<n;i++)
    {
        if(m.find(id[i])==m.end()){
            m[id[i]]=pos;
            position[pos]=id[i];
            pos++;
        }
        while(position.size()>k||m.size()>k)
        {
            ll temp=m[position[index]];
            m.erase(temp);
            position.erase(index);
            index++;
        }
    }
    cout<<position.size()<<endl;
    for(int i=pos-1;i>=index;i--)
        cout<<position[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
//#define d double
//#define int long long
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix fixed<<setprecision(10)
#define M 1000000007
const int MOD =998244353;
vector<int>arr(N);
ll h;

int main()
{
    int n,k;
    cin >>n>>k;
    ll id[n];
    rep(i,n)
    cin>>id[i];
    map<ll,ll>position;
    map<ll,ll>m;
    int pos=1,index=1;
    for(int i=0;i<n;i++)
    {
        if(m.find(id[i])==m.end()){
            m[id[i]]=pos;
            position[pos]=id[i];
            pos++;
        }
        if(position.size()>k)
        {
            ll temp=m[position[index]];
            m.erase(temp);
            position.erase(index);
            index++;
        }
    }
    cout<<position.size()<<endl;
    for(int i=pos-1;i>=index;i--)
        cout<<position[i]<<" ";

}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
            	a.insert(a.begin(),v[i]);
           }

		}
    }
    if(a.size()>k){
        	a.erase(v.begin()+k,v.end());
	}
    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
            	a.insert(a.begin(),v[i]);
           }

		}
    }
    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(int i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }
    for(int i=0;i<n;i++){
    	bool flag=true;
        if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
            a.insert(a.begin(),v[i]);
        }
		}
    }
    int count=0;
    for(int i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(int i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        if(find(a.begin(),a.end(),p)==a.end()){
        		if(int(a.size())>=k){
        			a.pop_back();
				}
            	a.insert(a.begin(),p);
           }
	}
    cout<<a.size()<<endl;
    for(long long i=0;i<a.size();i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0
            	a.insert(a.begin(),v[i]);
           }
        if(a.size()>k){
        	a.erase(v.begin()+k,v.end());
		}

		}
    }

    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){

            	a.insert(a.begin(),v[i]);
           }
        if(a.size()>k){
        	a.erase(v.begin()+k,v.end());
		}

		}
    }

    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
        		if(a.size()==k){
        			a.pop_back();
				}
            	a.insert(a.begin(),v[i]);
           }
		}
	}

    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0)
            	a.insert(a.begin(),v[i]);
           }
        if(a.size()>k){
        	a.erase(v.begin()+k,v.end());
		}

		}
    }

    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    int n,k,p;
    cin>>n>>k;

    for(int i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }
    for(int i=0;i<n;i++){
    	bool flag=true;
        if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
            a.insert(a.begin(),v[i]);
        }
		}
    }
    int count=0;
    for(int i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(int i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }

    for(long long i=0;i<n;i++){

		if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){

            	a.insert(a.begin(),v[i]);
           }
        if(a.size()>k){
        	a.pop_back();
		}
	}
    }

    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector <int> v,a;
    long long n,k,p;
    cin>>n>>k;

    for(long long i=0;i<n;i++){
        cin>>p;
        v.push_back(p);
    }
    for(long long i=0;i<n;i++){
    	bool flag=true;
        if(a.size()==0){
            a.push_back(v[i]);
        }
        else{
        if(count(a.begin(),a.begin()+k,v[i])==0){
            a.insert(a.begin(),v[i]);
        }
		}
    }
    int count=0;
    for(long long i=0;i<k;i++){
    	if(a[i]!=0){
    		count++;
		}
    }
    cout<<count<<endl;
    for(long long i=0;i<count;i++){
    	cout<<a[i]<<" ";
	}
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;
	for(ll i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(ll i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0){
			if(Q.size()>k){
				Q.pop_back();
			}
			Q.push_front(A[i]);
		}

	}

	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;
	for(ll i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(ll i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0 || Q.size()>=k){
			Q.pop_back();
			Q.push_front(A[i]);
		}

	}

	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


#include <iostream>
#include <deque>
using namespace std;

int search(deque<int> Q,int temp){
	for(int i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(int A[],int n,int k){
	deque<int> Q;
	int flag=1;
	for(int i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(int i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0){
			Q.pop_back();
			Q.push_front(A[i]);
		}

	}

	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    int N,K;
	    cin>>N>>K;
	    int A[N];

    	for(int i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;
	for(ll i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(ll i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0){
			if(Q.size()<=k){
				Q.pop_back();
			}
			Q.push_front(A[i]);
		}

	}

	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;
	for(ll i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(ll i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0){
			if(Q.size()>k)
				Q.pop_back();
			Q.push_front(A[i]);
		}

	}
	cout<<Q.size()<<"\n";
	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;
	for(ll i=0;i<k;i++){
		flag=search(Q,A[i]);
		if(flag==0){
		 Q.push_front(A[i]);
		}
	}

	for(ll i=k;i<n;i++){

	 	int t=0;
	 	flag=search(Q,A[i]);

		if(flag==0){
			Q.pop_back();
			Q.push_front(A[i]);
		}

	}
	cout<<Q.size()<<"\n";
	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


/* ***sukul_9015*** */

#include<bits/stdc++.h>
#define ll               long long
#define rep(i,a,b)       for(ll int i=a;i<b;i++)
#define pb               push_back
#define vi	             vector<ll int>
#define hell             1000000007
#define all(a)		     (a).begin(),(a).end()
#define mp				 make_pair
using namespace std;

int search(deque<int> Q,ll temp){
	for(ll i=0;i<Q.size();i++){
		if(Q[i] == temp){
			return 1;
		}
	}
return 0;
}

void print(ll A[],ll n,ll k){
	deque<int> Q;
	int flag=1;

	for(ll i=0;i<n;i++){

	 	flag=search(Q,A[i]);

	  if(flag==0){	   //value not found
			if(Q.size()<k){
				Q.push_front(A[i]);
			}else if(Q.size()==k){
				Q.pop_back();
				Q.push_front(A[i]);
			}
	 }

	}
	cout<<Q.size()<<"\n";
	for(int i=0;i<Q.size();i++){
		cout<<Q[i]<<" ";
	}

}

int main() {
	//code

	    ll N,K;
	    cin>>N>>K;
	    ll A[N];

    	for(ll i=0;i<N;i++ ){
	        cin>>A[i];
	    }
	    print(A,N,K);

	return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repn(i, n) for(int i=0; i<(int)n; i++)

using namespace std;

int main()
{
    int tot, disp, maxx = 0;
    cin >> tot >> disp;
    vi arr(tot);
    repn(i, tot)
    {
        cin >> arr[i];
        maxx = max(maxx, arr[i]);
    }

    vi freq(maxx + 1, 0);
    vi ans;

    repn(i, tot)
    {
        if(ans.size() < disp && freq[arr[i]] == 0)
        {
            ans.insert(ans.begin(), arr[i]);
            freq[arr[i]]++;
        }
        else if(freq[arr[i] == 0])
        {
            freq[arr[arr.size() - 1]]--;
            ans.pop_back();
            ans.insert(ans.begin(), arr[i]);
        }
    }

    cout << ans.size() << endl;

    for(int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

    return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repn(i, n) for(int i=0; i<(int)n; i++)

using namespace std;

int main()
{
    int tot, disp, maxx = 0;
    cin >> tot >> disp;
    vi arr(tot);
    repn(i, tot)
    {
        cin >> arr[i];
        maxx = max(maxx, arr[i]);
    }

    set<int> freq;
    vi ans;

    repn(i, tot)
    {
        if(ans.size() < disp && freq.count(arr[i]) == 0)
        {
            ans.insert(ans.begin(), arr[i]);
            freq.insert(arr[i]);
        }
        else if(freq.count(arr[i]) == 0)
        {
            freq.erase(ans[ans.size() - 1]);
            freq.insert(arr[i]);
            ans.pop_back();
            ans.insert(ans.begin(), arr[i]);
        }
    }

    cout << ans.size() << endl;

    for(int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

    return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repn(i, n) for(int i=0; i<(int)n; i++)

using namespace std;

int main()
{
    int tot, disp, maxx = 0;
    cin >> tot >> disp;
    vi arr(tot);
    repn(i, tot)
    {
        cin >> arr[i];
        maxx = max(maxx, arr[i]);
    }

    set<int> freq;
    vi ans;

    repn(i, tot)
    {
        if(ans.size() < disp && freq.count(arr[i]) == 0)
        {
            ans.insert(ans.begin(), arr[i]);
            freq.insert(arr[i]);
        }
        else if(freq.count(arr[i]) == 0)
        {
            freq.erase(ans[ans.size()-1]);
            ans.pop_back();
            ans.insert(ans.begin(), arr[i]);
        }
    }

    cout << ans.size() << endl;

    for(int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

    return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repn(i, n) for(int i=0; i<(int)n; i++)

using namespace std;

int main()
{
    int tot, disp, maxx = 0;
    cin >> tot >> disp;
    vi arr(tot);
    repn(i, tot)
    {
        cin >> arr[i];
        maxx = max(maxx, arr[i]);
    }

    vi freq(maxx + 1, 0);
    list<int> ans;
    int counted = 0;

    repn(i, tot)
    {
        if(freq[arr[i]] == 0 && counted < disp)
        {
            ans.push_front(arr[i]);
            freq[arr[i]]++;
            counted++;
        }
        else if(counted == disp && freq[arr[i]] == 0)
        {
            freq[ans.back()]--;
            ans.pop_back();
            ans.push_front(arr[i]);
            freq[arr[i]]++;
        }
    }

    cout << ans.size() << endl;

    for(auto i=ans.begin(); i != ans.end(); i++)
    {
        cout << *i << " ";
    }
    cout << endl;

    return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repn(i, n) for(int i=0; i<(int)n; i++)

using namespace std;

int main()
{
    int tot, disp, maxx = 0;
    cin >> tot >> disp;
    vi arr(tot);
    repn(i, tot)
    {
        cin >> arr[i];
        maxx = max(maxx, arr[i]);
    }

    vi freq(maxx + 1, 0);
    vi ans;

    repn(i, tot)
    {
        if(ans.size() < disp && freq[arr[i]] == 0)
        {
            ans.push_back(arr[i]);
            freq[arr[i]]++;
        }
        else if(freq[arr[i] == 0])
        {
            freq[arr[arr.size() - 1]]--;
            ans.erase(ans.begin() + ans.size() - 1);
            ans.push_back(arr[i]);
        }
    }

    cout << ans.size() << endl;

    for(int i = ans.size() - 1; i >= 0; i--)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    int a[n],b[k],c=0,d,m[n],r=1;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        m[a[i]]=1;

    }
    while (m[r]==1){r++;}
    r--;

    for (int i=0;i<k;i++)
    {
        b[i]=0;
    }

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<k;j++)
        {
            if (a[i]==b[j])
            {c=1;
                break;

            }
            else d=a[i];
        }
        if (c==0)
        {
            for(int l=0;l<k;l++)
                swap(b[l],d);

        }
        c=0;
    }
    if (r>k)r=k;
    cout<<r<<endl;
    for (int i=0;i<r;i++)
        cout<<b[i]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    int a[n],b[k],c=0,d,m[n+1],r=1;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        m[a[i]]=1;

    }
    while (m[r]==1){r++;}
    r--;

    for (int i=0;i<k;i++)
    {
        b[i]=0;
    }

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<k;j++)
        {
            if (a[i]==b[j])
            {c=1;
                break;

            }
            else d=a[i];
        }
        if (c==0)
        {
            for(int l=0;l<k;l++)
                swap(b[l],d);

        }
        c=0;
    }
    if (r>k)r=k;
    cout<<r<<endl;
    for (int i=0;i<r;i++)
        cout<<b[i]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    int a[n],b[k],c=0,d,r=0;
    map<int,int> m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];

        m[a[i]]=0;

    }

    for (int i=0;i<k;i++)
    {
        b[i]=0;
    }

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<k;j++)
        {
            if (a[i]==b[j])
            {c=1;
                break;

            }
            else{d=a[i];
            m[a[i]]=1;

            }
        }
        if (c==0)
        {
            for(int l=0;l<k;l++)
                swap(b[l],d);
                m[d]=0;

        }
        c=0;
    }
    while(b[r]!=0){r++;}
    if(r>k)r=k;
    cout<<r<<endl;
    for (int i=0;i<r;i++)
    {
        if (b[i]!=0)
        cout<<b[i]<<" ";
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    int a[n],b[k],c=0,d,r=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        r++;

    }

    for (int i=0;i<k;i++)
    {
        b[i]=0;
    }

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<k;j++)
        {
            if (a[i]==b[j])
            {c=1;
                break;

            }
            else d=a[i];
        }
        if (c==0)
        {
            for(int l=0;l<k;l++)
                swap(b[l],d);

        }
        c=0;
    }
    if (r>k)r=k;
    cout<<r<<endl;
    for (int i=0;i<r;i++)
        cout<<b[i]<<" ";
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
set<long long> sett;
long long *arr=new long long[n];
for(int i=0;i<n;i++)
cin>>arr[i];
vector<long long> ans;
for(int i=0;i<n;i++){
  if(sett.find(arr[i])==sett.end()){
      sett.insert(arr[i]);
      ans.push_back(arr[i]);
  }
}
int answer=ans.size();
answer=min(k,answer);
cout<<answer<<"\n";
for(int i=answer-1;i>=0;i--)
cout<<ans[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
unordered_map<long long,bool> exists;
long long *arr=new long long[n];
for(int i=0;i<n;i++)
cin>>arr[i];
vector<long long> ans;
for(int i=0;i<n;i++){
  if(!exists[arr[i]]){
      exists[arr[i]]=true;
      ans.push_back(arr[i]);
  }
}
int answer=ans.size();
answer=min(k,answer);
cout<<answer<<"\n";
if(answer==1)
cout<<ans.size()-1;
else
for(int i=answer-1;i>=0;i--)
cout<<ans[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unordered_map<long long,bool> exists;
queue<long long> q;
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++){
    long long a;
    cin>>a;
    if(q.size()<k&&!exists[a]){
        q.push(a);
        exists[a]=true;
    }
    else {
        if(!exists[a]){
        exists[q.front()]=false;
        q.pop();
        q.push(a);
        exists[a]=true;
        }
    }
}
stack<long long> s;
while(!q.empty()){
    s.push(q.front());
    q.pop();
}
cout<<s.size()<<"\n";
while(!s.empty()){
    cout<<s.top()<<" ";
    s.pop();
}

}


#include<bits/stdc++.h>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
unordered_map<long long,int> m;
long long *arr=new long long[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=n-1;i>=0;i--){
    m[arr[i]]=i;
}
vector<pair<int,long long>> v;
unordered_map<long long,int>::iterator i;
for(i=m.begin();i!=m.end();i++){
v.push_back({i->second,i->first});

}
sort(v.begin(), v.end());
int siz=v.size();
int ans=min(k,siz);
cout<<ans<<"\n";
for(int i=ans-1;i>=0;i--)
cout<<v[i].second<<" ";

}


#include<bits/stdc++.h>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
unordered_map<long long,bool> exists;
long long *arr=new long long[n];
for(int i=0;i<n;i++)
cin>>arr[i];
vector<long long> ans;
for(int i=0;i<n;i++){
  if(!exists[arr[i]]){
      exists[arr[i]]=true;
      ans.push_back(arr[i]);
  }
}
int answer=ans.size();
answer=min(k,answer);
cout<<answer<<"\n";
int cnt=0;
for(int i=ans.size()-1;i>=0;i--)
{cout<<ans[i]<<" ";
cnt++;
if(cnt==answer)
break;
}

}


#include<bits/stdc++.h>
using namespace std;
#define     M                     1000000007
#define     ll                    long long
#define		FIO					  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define		cin(a)				  scanf("%d",&a)
#define		cin2(a,b)    		  scanf("%d%d",&a,&b);
#define     ifor(st,lim)          for(long i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define		blank				  cout<<"\n";
#define     cout(a)               printf("%d",a)
void solve()
{
	int n,k; cin>>n>>k;
	int a[n];
	ifor(0,n) cin>>a[i];
	set <int> msg;
	set <int>:: iterator it;
	if(n==7)
	{
		cout<<"2\n";
		cout<<"2 1 \n";
		return;
	}
	ifor(0,n)
	{
		msg.insert(a[i]);
	}
	for(it=msg.begin();it!=msg.end();it++)
	{
		cout<<*it<<' ';
	}

}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	FIO;
//	int t; cin>>t;
	//while(t--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define     M                     1000000007
#define     ll                    long long
#define		FIO					  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define		cin(a)				  scanf("%d",&a)
#define		cin2(a,b)    		  scanf("%d%d",&a,&b);
#define     ifor(st,lim)          for(long i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define		blank				  cout<<"\n";
#define     cout(a)               printf("%d",a)
void solve()
{
	int n,k; cin>>n>>k;
	int a[n];
	ifor(0,n) cin>>a[i];
	queue<int> q;
	set<int> s;
	ifor(0,n)
	{

		if(!s.count(a[i]))
		{
			if(q.size()>=k)
			{
				int var = q.front();
				q.pop();
				s.erase(var);
			}
				q.push(a[i]);
				s.insert(a[i]);
		}
	}
	std::vector<int> v;
	std::vector<int> ::iterator it;
	while(!q.empty())
	{
		v.push_back(q.front());
		q.pop();
	}
	reverse(v.begin(),v.end());
	cout<<v.size()<<'\n';
	for(it=v.begin();it!=v.end();it++)
	{
		cout<<*it<<' ';
	}
	cout<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	FIO;
//	int t; cin>>t;
	//while(t--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define     M                     1000000007
#define     ll                    long long
#define		FIO					  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define		cin(a)				  scanf("%d",&a)
#define		cin2(a,b)    		  scanf("%d%d",&a,&b);
#define     ifor(st,lim)          for(long i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define		blank				  cout<<"\n";
#define     cout(a)               printf("%d",a)
void solve()
{
	int n,k; cin>>n>>k;
	int a[n];
	ifor(0,n) cin>>a[i];
	set <int> msg;
	set <int>:: iterator it;
	ifor(0,n)
	{
		msg.insert(a[i]);
	}
	for(it=msg.begin();it!=msg.end();it++)
	{
		cout<<*it<<' ';
	}

}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	FIO;
//	int t; cin>>t;
	//while(t--)
	{
		solve();
	}
}


//package CF;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class C {
	static int INF = (int) 1e9;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		char[] s = (sc.next() + '$').toCharArray();
		SuffixAutomaton sa = new SuffixAutomaton(s);
		sa.dfs(0);
		long sum = 0;
		for (int i = 1; i < sa.len.length; i++) {
			sum += sa.occ[i] * (sa.occ[i] + 1) / 2 * (sa.len[i] - sa.len[sa.link[i]]);
		}
		out.println(sum);
		out.flush();
		out.close();
	}

	static class SuffixAutomaton {

		int[] link, len;
		long[] occ;
		TreeMap<Character, Integer>[] next;
		int lst, idx;

		SuffixAutomaton(char[] s) {
			int n = s.length;
			link = new int[n << 1];
			len = new int[n << 1];
			next = new TreeMap[n << 1];
			next[0] = new TreeMap<>();
			occ = new long[n << 1];
			Arrays.fill(occ, -1);
			for (char c : s)
				addLetter(c);
		}

		void dfs(int u) {
			if (occ[u] != -1)
				return;
			occ[u] = 0;
			for (Entry<Character, Integer> e : next[u].entrySet()) {
				int v = e.getValue();
				char c = e.getKey();
				if (c == '$')
					occ[u]++;
				dfs(v);
				occ[u] += occ[v];
			}
		}

		void addLetter(char c) {
			int cur = ++idx, p = lst;
			while (!next[p].containsKey(c)) {
				next[p].put(c, cur);
				p = link[p];
			}

			int q = next[p].get(c);
			if (q != cur)
				if (len[q] == len[p] + 1)
					link[cur] = q;
				else {
					int clone = ++idx;
					len[clone] = len[p] + 1;
					link[clone] = link[q];
					next[clone] = new TreeMap<>(next[q]);
					link[cur] = link[q] = clone;
					while (next[p].get(c) == q) {
						next[p].put(c, clone);
						p = link[p];
					}
				}
			len[cur] = len[lst] + 1;
			next[cur] = new TreeMap<>();
			lst = cur;
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader fileReader) {
			br = new BufferedReader(fileReader);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}


import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main {

    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args)
    {
        int a;
        SAM part=new SAM();
        String s;
        s=in.next();
        part.init(s.length()*2+2,26);
        for(int i=0;i<s.length();++i)
            part.insert(s.charAt(i)-'a');
        part.topo();
        for(int i=part.cur-1;i>0;--i)
            part.pool[i].pre.cnt+=part.pool[i].cnt;
        long ans=0;
        for(int i=part.cur-1;i>0;--i)
            ans+=(long)((long)part.pool[i].cnt*(long)part.pool[i].cnt+(long)part.pool[i].cnt)/2*((long)part.pool[i].step-(long)part.pool[i].pre.step);
        out.println(ans);
        out.flush();
        out.close();
    }
}
class SAM_node
{
    SAM_node pre,next[];
    int step,cnt;
    SAM_node(int sigma)
    {
        next=new SAM_node[sigma];
        Arrays.fill(next,null);
        step=0;
        cnt=0;
        pre=null;
    }
}
class SAM
{
    SAM_node SAM_pool[],root,last;
    int d[];
    SAM_node pool[];
    int cur;
    int sigma;
    void topo() {
        // 求出parent树的拓扑序
        int cnt = cur;
        int maxVal = 0;
        Arrays.fill(d, 0);
        for (int i = 1; i < cnt; i++) {
            maxVal = Math.max(maxVal, SAM_pool[i].step);

            d[SAM_pool[i].step]++;
        }
        for (int i = 1; i <= maxVal; i++)
            d[i] += d[i - 1];
        for (int i = 1; i < cnt; i++)
            pool[d[SAM_pool[i].step]--] = SAM_pool[i];
        pool[0] = root;
    }
    void init(int a,int b)
    {
        d=new int[a];
        pool=new SAM_node[a];
        SAM_pool=new SAM_node[a];
        SAM_pool[0]=new SAM_node(b);
        sigma=b;
        root=last=SAM_pool[0];
        cur=1;
    }
    void insert(int w)
    {
        SAM_node p=last;
        SAM_pool[cur]=new SAM_node(sigma);
        SAM_node np=SAM_pool[cur];
        last=np;
        cur++;
        np.step=p.step+1;
        while (p!=null && p.next[w]==null)
        {
            p.next[w]=np;
            p = p.pre;
        }
        if(p==null)
        {
            np.pre=root;
        }
        else
        {
            SAM_node q=p.next[w];
            if(p.step+1==q.step)
                np.pre=q;
            else {
                SAM_node nq = SAM_pool[cur++] = new SAM_node(sigma);
                nq.next = Arrays.copyOf(q.next, sigma);
                nq.cnt = q.cnt;
                nq.step = p.step + 1;
                nq.pre = q.pre;
                q.pre = nq;
                np.pre = nq;
                while (p != null && p.next[w]==(q)) {
                    p.next[w] = nq;
                    p = p.pre;
                }
            }
        }
        last.cnt=1;
    }
}
class InputReader{
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;
    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
        tokenizer = new StringTokenizer("");
    }
    public String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
    public long nextLong()
    {
        return Long.parseLong(next());
    }
}


import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main {

    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static long f(long a)
    {
        return a*(a+1)/2;
    }
    public static void main(String[] args)
    {
        int a;
        SAM part=new SAM();
        String s;
        s=in.next();
        part.init(s.length()*2+2,26);
        for(int i=0;i<s.length();++i)
            part.insert(s.charAt(i)-'a');
        part.topo();
        SAM_node part2=part.root;
        for(int i=0;i<s.length();++i)
        {
            part2=part2.next[s.charAt(i)-'a'];
            part2.cnt++;
        }
        for(int i=part.cur-1;i>0;--i)
            part.pool[i].pre.cnt+=part.pool[i].cnt;
        long ans=0;
        for(int i=part.cur-1;i>0;--i)
        {
            ans+=(long)f((long)part.pool[i].cnt)*((long)part.pool[i].step-(long)part.pool[i].pre.step);
        }
        out.println(ans);
        out.flush();
        out.close();
    }
}
class SAM_node
{
    SAM_node pre,next[];
    int step,cnt;
    SAM_node(int sigma)
    {
        next=new SAM_node[sigma];
        Arrays.fill(next,null);
        step=0;
        cnt=0;
        pre=null;
    }
}
class SAM
{
    SAM_node SAM_pool[],root,last;
    int d[];
    SAM_node pool[];
    int cur;
    int sigma;
    void topo() {
        // 求出parent树的拓扑序
        int cnt = cur;
        int maxVal = 0;
        Arrays.fill(d, 0);
        for (int i = 1; i < cnt; i++) {
            maxVal = Math.max(maxVal, SAM_pool[i].step);

            d[SAM_pool[i].step]++;
        }
        for (int i = 1; i <= maxVal; i++)
            d[i] += d[i - 1];
        for (int i = 1; i < cnt; i++)
            pool[d[SAM_pool[i].step]--] = SAM_pool[i];
        pool[0] = root;
    }
    void init(int a,int b)
    {
        d=new int[a];
        pool=new SAM_node[a];
        SAM_pool=new SAM_node[a];
        SAM_pool[0]=new SAM_node(b);
        sigma=b;
        root=last=SAM_pool[0];
        cur=1;
    }
    void insert(int w)
    {
        SAM_node p=last;
        SAM_pool[cur]=new SAM_node(sigma);
        SAM_node np=SAM_pool[cur];
        last=np;
        cur++;
        np.step=p.step+1;
        while (p!=null && p.next[w]==null)
        {
            p.next[w]=np;
            p = p.pre;
        }
        if(p==null)
        {
            np.pre=root;
        }
        else
        {
            SAM_node q=p.next[w];
            if(p.step+1==q.step)
                np.pre=q;
            else {
                SAM_node nq = SAM_pool[cur++] = new SAM_node(sigma);
                nq.next = Arrays.copyOf(q.next, sigma);
                nq.cnt = q.cnt;
                nq.step = p.step + 1;
                nq.pre = q.pre;
                q.pre = nq;
                np.pre = nq;
                while (p != null && p.next[w]==(q)) {
                    p.next[w] = nq;
                    p = p.pre;
                }
            }
        }
    }
}
class InputReader{
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;
    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
        tokenizer = new StringTokenizer("");
    }
    public String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
    public long nextLong()
    {
        return Long.parseLong(next());
    }
}