#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	char a[10000],c[10000],b[10000];
	gets(a);
	strcpy(c,a);
	strrev(c);
	if(strcmp(c,a)==0)
	{
		puts(c);
	}
	else
	{
		strcat(a,c);
		puts(a);
	}

}


#include<stdio.h>
#include<string.h>
int main()
{
    char n[100000],m[100000];
    int i,k,x;
    scanf("%s",n);
    k=strlen(n);
    strcpy(m,n);
    strrev(m);
    x=strcmp(n,m);
    if(x==0) printf("%s",n);
    else
    {
        printf("%s%s",n,m);
    }
}


#include<stdio.h>
#include<string.h>

int main()
{
    char str[100000],str2[100000];
    scanf("%s",str);

    strcpy(str2,str);
    strrev(str2);
    strcat(str,str2);
    printf("%s",str);

}


#include<stdio.h>
#include<string.h>
#define N 1001
#define M 10001
int main(){
    char a[N];
    char l[M];
    char r[M];
    scanf("%s",a);
    int i,j;
    int na=strlen(a);
    int nl=0;
    int nr=0;
    i=0,j=na-1;
    while(1){
        if(i<na && j>=0 && i<=j){
            if(a[i]==a[j]){
                l[nl]=a[i];
                nl++;
                r[nr]=a[j];
                nr++;
                i++;
                j--;
            }else{
                l[nl]=a[i];
                nl++;
                r[nr]=a[i];
                nr++;
                i++;
            }
        }else{
            break;
        }
    }
    if(nl+nr>10000){
        printf("超了\n");
        return 1;
    }
    for(i=0;i<nl;i++){
        printf("%c",l[i]);
    }
    for(i=nr-1;i>=0;i--){
        printf("%c",r[i]);
    }
    printf("\n");

    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[1000],temp[1000],b[10000];
	int n,flag=0,i,j;
	scanf("%s", a);
	n=strlen(a);
	for(i=0,j=n-1;i<n;i++,j--)
	{
		if(a[i]==a[j])
		{
			continue;
		}
		else
		{
			flag=1;
			break;
		}
	}
	if(flag==0)
	{
		printf("%s",a);
	}
	else
	{
		for(i=0;i<n;i++)
		{
			b[i]=a[i];
		}
		for(j=n-1;j>=0;j--)
		{
			b[i]=a[j];
			i++;
		}
		for(j=0;j<i;j++)
		{
		    printf("%c",b[j]);
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[1000],temp[1000],b[10000];
	int n,flag=0,i,j;
	scanf("%s", a);
	n=strlen(a);
	for(i=0,j=n-1;i<n;i++,j--)
	{
		if(a[i]==a[j])
		{
			continue;
		}
		else
		{
			flag=1;
			break;
		}
	}
	if(flag==0)
	{
		printf("%s",a);
	}
	else
	{
		for(i=0;i<n;i++)
		{
			b[i]=a[i];
		}
		for(j=n-1;j>=0;j--)
		{
			b[i]=a[j];
			i++;
		}
		for(j=0;j<=i;j++)
		{
		    printf("%c",b[j]);
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	int i,n;
	char A[1010], B[3000] = "";
        scanf("%s", A);
	n=strlen(A);
	strcpy(B,A);
  	for (i=0; i<n; i++)
		B[n+i] = A[n-1-i];
	B[n+i]='\0';
	printf("%s\n",B);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	int i;
	char A[1010], B[3000] = "",n;
        scanf("%s", A);
	n=strlen(A);
	strcpy(B,A);
  	for (i=0; i<n; i++)
		B[n+i] = A[n-1-i];
	B[n+i]='\0';
	printf("%s\n",B);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char A[1010], B[3000] = "";
        scanf("%s", A);
	strcpy(B,A);
	strcat(B,A);
	printf("%s\n",B);
	return 0;
}


#include<stdio.h>
#include<string.h>
char s[5010];
int len;

int main()
{
    scanf("%s",s + 1);
    len = strlen(s + 1);
    for (int i = 1; i <= len; i++)
        s[i + len] = s[len - i + 1];
    for (int i = 1; i <= len * 2; i++)
        printf("%c",s[i]);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char a[1100];
   int n, i;

   scanf("%s", a);
   n = strlen(a);

   printf("%s", a);
   for(i=n-1; i>=0; i--)
   {
       printf("%c", a[i]);
   }
}


#include <stdio.h>
#include <string.h>
int main() {
    char word[1000];
    scanf("%s",word);
    int len=strlen(word);
    printf("%s",word);
    for (int i=1; i<len; i++) printf("%c",word[len-1-i]);
    return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
    char str[1000];
    scanf("%s",str);
    int len = strlen(str);

    printf("%s",str);
    for(int i =len-1; i >= 0; i--)
    {
        printf("%c",str[i]);
    }
    printf("\n");
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char a[1001];
    gets(a);
    printf("%s%s\n",a,strrev(a));
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char a[1001];
    gets(a);
    printf("%s",a);
    printf("%s\n",strrev(a));
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[100],k[201];
    int i,j=1;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        k[i]=s[i];
    for(i=strlen(s);i<2*strlen(s);i++)
        {
          k[i]=s[strlen(s)-j];
          j++;
        }
        k[i]=0;
    printf("%s",k);
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[1000],k[2000];
    int i,j=1;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        k[i]=s[i];
    for(i=strlen(s);i<2*strlen(s);i++)
        {
          k[i]=s[strlen(s)-j];
          j++;
        }
        k[i]=0;
    printf("%s",k);
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[1001],k[10000];
    int i,j=1;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        k[i]=s[i];
    for(i=strlen(s);i<2*strlen(s);i++)
        {
          k[i]=s[strlen(s)-j];
          j++;
        }
        k[i]=0;
    printf("%s",k);
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[100],k[10000];
    int i,j=1;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        k[i]=s[i];
    for(i=strlen(s);i<2*strlen(s);i++)
        {
          k[i]=s[strlen(s)-j];
          j++;
        }
        k[i]=0;
    printf("%s",k);
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[100],k[10000];
    int i,j;
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
        k[i]=s[i];
    for(i=strlen(s),j=1;i<2*strlen(s);i++)
        {
          k[i]=s[strlen(s)-j];
          j++;
        }
        k[i]=0;
    printf("%s",k);
}


#include<stdio.h>
#include<string.h>
int main()
{
    int n,m,i,j,k,l;
    char c[10009],d[10009];
    scanf("%s",&c);
    n=strlen(c);
    k=n;
    printf("%s",c);
    for(i=n-1;i>=0;i--)
        printf("%c",c[i]);
}


#include<stdio.h>
int main()
{
    char A[1001];
    int i=0;
    scanf("%s",&A);
    printf("%s",A);
    while(A[i]!=0)
    {
        i=i+1;
    }
    i=i-1;
    while(i>=0)
    {
        printf("%c",A[i]);
        i=i-1;
    }
}


#include<stdio.h>
#include<string.h>

int main()
{
    char A[10002],C[10002];

    scanf("%s",A);

    strcpy(C,A);
    strrev(C);

    strcat(A,C);

    printf("%s",A);

    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i;
    char a[1001];
    scanf("%s",a);
    if(strcmp(a,strrev(a))==0)
        printf("%s\n",a);
    else
        printf("%s%s\n",a,strrev(a));
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i;
    char a[1001],b[1001];
    scanf("%s",a);
    strcpy(b,a);
    if(strcmp(a,strrev(b))==0)
        printf("%s\n",a);
    else
        printf("%s%s\n",a,b);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,l;
    char s[1000000];
    scanf("%s",s);
    l = strlen(s);
    n = strcmp((strrev(s)),s);
    if(n==1)
    {
        printf("%s",s);
    }
    else
    {
        printf("%s",s);
        while(l!=0)
        {
            printf("%c",s[l-1]);
            l--;
        }
    }
}


#include<stdio.h>
int main()
{
    int i,n;
    char s[1001];
    scanf("%s",s);
    n=strlen(s);
    printf("%s",s);
    for(i=(n-1);i>=0;i--)
    {
        printf("%c",s[i]);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,c=0;
    char s[1000+3],r[1003],a[10004];
    scanf("%s",s);
    strcpy(r,s);
    strrev(r);
    strcat(s,r);
    if(strlen(s)<=5000)
        printf("%s\n",s);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,c=0;
    char s[1000+3],r[1003],a[10004];
    scanf("%s",s);
    strcat(r,s);
    strrev(r);
    if( strcmp(s,r)==0 )
    {
        printf("%s",s);
    }
    else
    {
        if(strlen(s)<=5000)
            printf("%s",strcat(s,r));
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,c=0;
    char s[1000+3],r[1003],a[10004];
    scanf("%s",s);
    strcat(r,s);
    strrev(r);
    if(strlen(s)<=5000)
        printf("%s\n",strcat(s,r));
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,c=0;
    char s[1000+3],r[1003],a[10004];
    scanf("%s",s);
    strcat(r,s);
    strrev(r);
    if( strcmp(s,r)==0 )
    {
        printf("%s\n",s);
        return 0;
    }
    else
    {
        if(strlen(s)<=5000)
            printf("%s\n",strcat(s,r));
    }
    return 0;
}


#include<stdio.h>

int main()
{
    char s[1001]={'\0'};
    scanf("%s",s);
    int n=strlen(s);
    printf("%s",s);
    for(int i=n-1;i>=0;i--)
    printf("%c",s[i]);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int num,m,sum1=0,sum2=0,c=0,i,j,c1=0;
    char s[10005],t[100005];
    scanf("%s",&s);
    strcpy(t,s);
    strrev(s);
    if(strcmp(s,t)==0)
    {
        printf("%s",s);
        return 0;
    }
    printf("%s%s",t,s);
}


#include<stdio.h>
#include<string.h>
int main()
{
    char S[1001];
    int i,k;
    scanf("%s",S);
    k=strlen(S);
    printf("%s",S);
    strrev(S);
    printf("%s\n",S);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int i;
	char a[2001];
	scanf("%s",a);
	printf("%s",a);
	for(i=strlen(a)-1;i>=0;--i)
	printf("%c",a[i]);
	return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1001],revs[10001];
    gets(s);
    int i=0,p;
    p=strlen(s);
    while(i<strlen(s))
    {
        revs[i]=s[p-1];
        i++;
        p--;
    }
    if(strcmp(s,revs)==0)
        printf("%s",s);

    else printf("%s%s",s,revs);

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1001],revs[1001];
    gets(s);
    int i=0,p;
    p=strlen(s);
    while(i<strlen(s))
    {
        revs[i]=s[p-1];
        i++;
        p--;
    }
    if(strcmp(s,revs)==0)
        printf("%s",s);

    else printf("%s%s",s,revs);

    return 0;
}


#include<stdio.h>
#include<string.h>
int main(void)
{
    char string[1005];
    scanf("%s",string);
    int i,length=strlen(string),limit;
    printf("%s",string);
    for(i=length-1;i>=0;i--)
    {
        printf("%c",string[i]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int size;
char s[1001];

void reverseprint ()
{
	for (int i=size-1;i>=0;i--)
	{
		printf("%c",s[i]);
	}
}

void normalprint ()
{
	for(int i=0;i<size;i++)
	{
		printf("%c",s[i]);
	}
}

int main ()
{
	//freopen ("cf.in","r",stdin),freopen("cf.out","w",stdout);
	scanf ("%d %s",&size,s);
	reverseprint();
	normalprint();
}


#include <stdio.h>
#include <string.h>

int size;
char s[1001];

void reverseprint ()
{
	for (int i=strlen(s)-1;i>=0;i--)
	{
		printf("%c",s[i]);
	}
}

void normalprint ()
{
	for(int i=0;i<strlen(s);i++)
	{
		printf("%c",s[i]);
	}
}

int main ()
{
	//freopen ("cf.in","r",stdin),freopen("cf.out","w",stdout);
	scanf ("%s",s);
	reverseprint();
	normalprint();
}


//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var s = next();
	var output = s;
	for(var i = 0; i < s.length; i++){
		output += s[s.length - 1 - i];
	}
	myout(output);
}


const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', (str) => {
  const ans = str + [...str].reverse().join('');
  console.log(ans);
});


using System;

namespace codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = Console.ReadLine();
            char[] ch = a.ToCharArray();
            Array.Reverse(ch);
            a += string.Join("", ch);

            Console.WriteLine(a);
        }
    }
}


using System;
class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            string copy = null;
            {
                char[] ar = new char[line.Length];
                for (int i = 0, j = line.Length - 1; j>=0 ;)
                    ar[i++] = line[j--];
                copy = new string(ar);
            }
            Console.WriteLine(line+copy);
        }
    }


using System;
using System.Linq;
class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            string copy = line;
            copy.Reverse();
            Console.WriteLine(line+copy);
        }
    }


using System;
using System.Linq;

namespace CSharp
{
    public class _932A
    {
        public static void Main()
        {
            string a = Console.ReadLine();
            Console.WriteLine(a + new string(a.Reverse().ToArray()));
        }
    }
}


using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string s = Console.ReadLine();
        Console.WriteLine(s + new string(s.Reverse().ToArray()));
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace AlgoTasks
{
    class Program
    {
        static void Main(string[] args)
        {
            #region
            //10 5
            //2  | 7  | 3 | 9  | 8 | 7 | 9  | 7 | 1 | 9
            //buy|sell|buy|sell|   |buy|sell|   |buy|sell
            //Таким образом, суммарная прибыль(9 - 2) + (9 - 1) = 15.
            //int[] nk = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
            //int[] nk = new int[] { 10, 5 };

            //long[] p = new long[nk[0]];
            //long[] p = new long[] { 2 , 7 , 3 , 9 , 8 , 7 , 9 , 7 , 1 , 9 };
            /*for (int i = 0; i < nk[0]; i++)
            {
                p[i] = long.Parse(Console.ReadLine());
            }*/

            //long[] arrIdx1 = Array.ConvertAll(Enumerable.Range(0, nk[0]).ToArray(), a => (long)a);
            //long[] arrIdx2 = (long[])arrIdx1.Clone();

            //long[] arrAsc = (long[])p.Clone();
            //long[] arrDesc = (long[])p.Clone();

            //Array.Sort(arrAsc, arrIdx1);
            //Array.Sort(arrDesc, arrIdx2);
            //arrDesc = arrDesc.Reverse().ToArray();
            //arrIdx2 = arrIdx2.Reverse().ToArray();

            /*for (int i = 0; i < nk[0]; i++)
            {
                Console.Write(arrAsc[i] + " ");
            }

            Console.WriteLine();
            for (int i = 0; i < nk[0]; i++)
            {
                Console.Write(arrIdx1[i] + " ");
            }*/

            //int n = int.Parse(Console.ReadLine());
            //int[] f = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
            //int[] likes = new int[n + 1];

            //5
            //2 4 5 1 3
            //0 1 2 3 4 5
            //  4 1 5 2 3

            /*for (int i = 0; i < n; i++)
            {
                likes[f[f[i] - 1]] = f[i];
            }

            bool triangle = false;
            for (int i = 1; i <= n; i++)
            {
                if (likes[likes[likes[i]]] == i)
                {
                    triangle = true;
                    break;
                }
            }

            if (triangle)
                Console.WriteLine("YES");
            else
                Console.WriteLine("NO");*/
            #endregion

            string s = Console.ReadLine();
            if (IsPalindrome(s))
                Console.WriteLine(s);
            else
            {
                StringBuilder sb = new StringBuilder(s);
                for(int i = sb.Length - 2; i >= 0; i--)
                {
                    sb.Append(sb[i]);
                }
                Console.WriteLine(sb.ToString());
            }
        }

        static bool IsPalindrome(string s)
        {
            int i = 0;
            int j = s.Length - 1;
            while(i < j)
            {
                if (s[i] != s[j])
                    return false;
                i++;
                j--;
            }

            return true;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             char[]c=Console.ReadLine().ToCharArray();
             Console.Write(c);
             Array.Reverse(c);
             Console.Write(c);
        }
    }
}


using System;
using System.IO;
using System.Linq;
using System.Text;

namespace Palindromic_Supersequence
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            writer.WriteLine(Solve(args));
            writer.Flush();
        }

        private static string Solve(string[] args)
        {
            string s = reader.ReadLine();

            var b = new StringBuilder(s);
            foreach (char c in s.Reverse())
            {
                b.Append(c);
            }

            return b.ToString();
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){

		int N = S.Length;
		List<char> L = new List<char>();
		for(int i=0;i<N;i++) L.Add(S[i]);
		for(int i=0;i<N;i++) L.Add(S[N - 1 -i]);
		Console.WriteLine(new String(L.ToArray()));

	}
	String S;
	public Sol(){
		S = rs();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


using System;

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            char[] arr = s.ToCharArray();
            string s1 =s;
            for (int i=s.Length-1;i>=0;i--)
            {
                s1 = s1 + arr[i];
            }
            Console.WriteLine(s1);
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Codeforces
{
   class A
   {
      static void Main(string[] args)
      {
         string S = Console.ReadLine();

         S = S + "" +Reverse(S);

         Console.WriteLine(S);
      }

      static string Reverse( string s )
      {
          char[] charArray = s.ToCharArray();
          Array.Reverse( charArray );
          return new string( charArray );
      }

   }
}


using System;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            Console.Write($"a{str}");
            for (int i = str.Length - 1; i >=0; i--)
            {
                Console.Write(str[i]);
            }
            Console.Write("a");
        }
    }
}


using System;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            Console.Write($"a{str}");
            for (int i = str.Length - 1; i >=0; i++)
            {
                Console.Write(str[i]);
            }
            Console.Write("a");
        }
    }
}


using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class Problem
{
    static void Main()
    {
        //--------------------------------input--------------------------------//

        var s = Console.ReadLine().ToArray();

        //--------------------------------solve--------------------------------/

        var s2=s.Reverse();
        Console.WriteLine( String.Join("",s) + String.Join("",s2) );

    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Palindromic_Supersequence
{
    class Program
    {
        static void Main(string[] args)
        {
            string A = Console.ReadLine();
            string B = A;
            for (int i = A.Length - 1; i >= 0; i--)
            {
                B += A[i];
            }
            Console.WriteLine(B);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Palindromic_Supersequence
{
    class Program
    {
        static void Main(string[] args)
        {
            string A = Console.ReadLine();
            string B = A + A.Reverse();
            Console.WriteLine(B);
        }
    }
}


using System;
class Program
{
    static void Main()
    {
        string s = Console.ReadLine();
        for (int i = s.Length - 2; i >= 0; i--)
            s += s[i];
        Console.WriteLine(s);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            char[] arr = line.ToCharArray();
            //Array.Reverse(arr);
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                line += arr[i];
            }
            Console.WriteLine(line);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            char[] arr = line.ToCharArray();
            Array.Reverse(arr);
            string reverse = new string(arr);
            line += reverse;
            Console.WriteLine(line);

        }
    }
}


#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(overflowing_literals)]
#![allow(unused_must_use)]
#![allow(non_camel_case_types)]

use std::collections::*;
use std::io::*;
use std::cmp::{max, min, Ordering};
use std::f64::consts::*;
use std::mem::{swap, size_of_val, size_of};
use std::fs::File;
use std::rc::*;
use std::ascii::*;
use std::ops::*;
use std::num::*;
use std::process::*;
use std::ptr::*;

const INF: i32 = std::i32::MAX;
const LINF: i64 = std::i64::MAX;
const MOD: usize = 1000000007;

struct Input<Buf: Read> {
	buffer: Buf,
}

impl<Buf: Read> Input<Buf> {
	fn read_line(&mut self) -> String {
		let mut v = Vec::new();
		let mut x = [255u8];
		self.buffer.read(&mut x).expect("EOF");
		while x[0] < b' ' && x[0] != 255u8 {
			x[0] = 255u8;
			self.buffer.read(&mut x).expect("EOF");
		}
		while x[0] >= b' ' && x[0] != 255u8 {
			v.push(x[0]);
			x[0] = 255u8;
			self.buffer.read(&mut x).expect("EOF");
		}
		String::from_utf8(v).unwrap()
	}

	fn read_word(&mut self) -> String {
		let mut v = Vec::new();
		let mut x = [255u8];
		self.buffer.read(&mut x).expect("EOF");
		while x[0] <= b' ' && x[0] != 255u8 {
			x[0] = 255u8;
			self.buffer.read(&mut x).expect("EOF");
		}
		while x[0] > b' ' && x[0] != 255u8 {
			v.push(x[0]);
			x[0] = 255u8;
			self.buffer.read(&mut x).expect("EOF");
		}
		String::from_utf8(v).unwrap()
	}
}

macro_rules! read {
    () => {};
	($inf:ident, $t:ty) => ($inf.read_word().parse::<$t>().expect("Parsing error"));
	($inf:ident, $($t:ty),*) => (($($inf.read_word().parse::<$t>().expect("Parsing error")),*));
}
fn gcd<T>(mut a: T, mut b: T) -> T
	where T: ShrAssign + BitAnd<Output=T> + Ord + From<u8> + Clone + SubAssign + ShlAssign<usize> + Copy
{
	let one = T::from(1);
	let zero = T::from(0);
	let mut ans = 0;
	if a < b {
		swap(&mut a, &mut b);
	}
	while b > zero {
		if (a & one > zero) && (b & one > zero) {
			a -= b;
		} else if a & one > zero {
			b >>= one;
		} else if b & one > zero {
			a >>= one;
		} else {
			ans += 1;
			a >>= one;
			b >>= one;
		}
		if a < b {
			swap(&mut a, &mut b);
		}
	}
	a <<= ans;
	a
}

fn powmod<X, Y>(mut base: X, mut exp: Y, mm: X) -> X
	where X: Copy + Clone + Mul<Output=X> + Rem<Output=X> + From<u8>,
	      Y: ShrAssign + Copy + Clone + BitAnd<Output=Y> + From<u8> + Ord
{
	let mut res = X::from(1);
	while exp > Y::from(0) {
		if exp & Y::from(1) > Y::from(0) {
			res = res * base % mm;
		}
		base = base * base % mm;
		exp >>= Y::from(1);
	}
	res
}

fn main() {
	match std::env::var("home") {
		Ok(_x) => {
			let mut inf = Input { buffer: BufReader::new(File::open("input.txt").expect("File not found")) };
			let stdout = stdout();
			let ostr = stdout.lock();
			let mut ouf = BufWriter::new(ostr);
			solve(&mut inf, &mut ouf);
//			let stack_size = 67108864;
//			let thd = std::thread::Builder::new().stack_size(stack_size);
//			thd.spawn(move || solve(&mut inf, &mut ouf)).unwrap().join().unwrap();
		}
		Err(_e) => {
			let stdin = stdin();
			let istr = stdin.lock();
//			let istr = File::open("hull.in").expect("File not found");
			let mut inf = Input { buffer: BufReader::new(istr) };
			let stdout = stdout();
			let ostr = stdout.lock();
//			let ostr = File::create("hull.out").expect("File not created");
			let mut ouf = BufWriter::new(ostr);
			solve(&mut inf, &mut ouf);
//          let stack_size = 268_435_456 / 4 * std::mem::size_of::<usize>() / 256 * 64; // 64 MB
//			let thd = std::thread::Builder::new().stack_size(stack_size);
//			thd.spawn(move || solve(&mut inf, &mut ouf)).unwrap().join().unwrap();
		}
	}
}

fn solve<Inner: Read, Output: Write>(inf: &mut Input<Inner>, ouf: &mut Output) {
	let mut s: Vec<char> = inf.read_line().chars().collect();
	for &x in &s {
		write!(ouf, "{}", x);
	}
	s.reverse();
	for &x in &s {
		write!(ouf, "{}", x);
	}
}


fn main() {
    let mut input = String::new();
    use std::io::prelude::*;
    std::io::stdin().read_to_string(&mut input).unwrap();
    input.trim();

    let output: String = input.trim().chars().chain(input.trim().chars().rev()).collect();

    println!("{}", output);
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	string A; int n;
	cin >> A;
	n = A.length();
	cout<<A;
	for(int i = 1; i <= n; i++){
		cout << A[n-i];
	}
}


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

int main(){

string str,str1;

cin>>str;
str1=str;

reverse(str.begin(),str.end());
str1=str1+str;
cout<<str1<<endl;
return 0;

}


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

bool palindrome( string chaine)
{
    vector <string> tab ;
    string chaine2 ;

    int i = chaine.size()-1 ;
    while (i>=0)
    {
        chaine2= chaine2+ chaine[i];
        i--;
    }

         if (chaine2.compare(chaine) == 0)
           {
              return true ;
           }
        else return false ;

}
string inverse (string chaine)
{
    int x= chaine.size()-1;
    string resultat ;
    while (x >=0)
    {
        resultat = resultat+ chaine[x];
        x--;
    }
    return resultat;

}
string souschaine(string chaine)
{
    string resultat;
    int s= chaine.size();
    if (s%2==0)
    {
        resultat= chaine+inverse(chaine);

    }
    else
    {
        int i = chaine.size() ;
        string ch= chaine.substr(1, i);

        resultat= inverse(ch)+chaine;

    }
    return resultat;
}

int main()
{

   string chaine ;
   cin >> chaine;

   if (palindrome(chaine))
   {
       cout << chaine;
   }
   else
   {
       cout << souschaine(chaine);
   }
    return 0;
}


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

bool palindrome( string chaine)
{
    vector <string> tab ;
    string chaine2 ;

    int i = chaine.size()-1 ;
    while (i>=0)
    {
        chaine2= chaine2+ chaine[i];
        i--;
    }

         if (chaine2.compare(chaine) == 0)
           {
              return true ;
           }
        else return false ;

}
string souschaine(string chaine)
{
    string resultat;
    int s= chaine.size();
    if (s%2==0)
    {
        resultat= chaine+chaine;

    }
    else
    {
        int i = chaine.size() ;
        string ch= chaine.substr(1, i);
        i--;
        string ch2;
        while (i>=0)
        {
            ch2= ch2+ ch[i];
            i--;
        }
        resultat= ch2+chaine;

    }
    return resultat;
}

int main()
{

   string chaine ;
   cin >> chaine;

   if (palindrome(chaine))
   {
       cout << chaine;
   }
   else
   {
       cout << souschaine(chaine);
   }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define sz(a) int((a).size())
#define lld long long int
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
int main()
{
	string a;
	cin>>a;
	string b=a;
	for (int i = 0; i <= sz(b)/2; ++i)
	{
		if (b[i]!=b[sz(b)-i-1])
		{
			b.insert(b.end()-i,b[i]);
		}
	}
	cout<<b<<endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
#define dbg(x) cout <<#x<<" = "<<x<<" ;  "
#define dbgln(x) cout <<#x<<" = "<<x<<endl
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=1e6+7;
const double eps=0.00000001;
LL gcd(LL a, LL b) {return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a, T b) {
	T ret=1;
	for (; b; b>>=1ll,a=(LL)a*a%M)
		if (b&1) ret=(LL)ret*a%M;
	return ret;
}

int n,m;
int A[maxn],B[maxn];
vector<int> pos[maxn];
multiset<int> S;
multiset<int>::iterator it;
int ans,all;
int main() {
	int i;
	scanf("%d%d",&n,&m);
	FOR(i,1,n) scanf("%d",&A[i]),pos[A[i]].push_back(i);
	FOR(i,1,m) scanf("%d",&B[i]),all+=B[i];
	FOR(i,1,m){
		if (B[i]>(int)pos[i].size()) return 0*puts("-1");
		else B[i]--;
		if (B[i]!=-1) S.insert(pos[i][B[i]]);
	}
	ans=INF;
	FOR(i,1,n){
		int k=A[i];
		it=S.end();it--;
		// printf("%d : %d\n",i,*it);
		ans=min(ans,*it-i-all+1);
		it=S.lower_bound(pos[k][B[k]]);
		S.erase(it);
		// printf("erase: %d\n",pos[A[i]][B[A[i]]]);
		B[k]++;
		if (B[k]==(int)pos[k].size()) break;
		S.insert(pos[k][B[k]]);
		// printf("insert: %d\n",pos[A[i]][B[A[i]]]);
	}
	printf("%d\n",ans);
	return 0;
}
/*
8 3
3 3 1 2 2 1 1 3
3 1 1

*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
#define dbg(x) cout <<#x<<" = "<<x<<" ;  "
#define dbgln(x) cout <<#x<<" = "<<x<<endl
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=2e5+7;
const double eps=0.00000001;
LL gcd(LL a, LL b) {return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a, T b) {
	T ret=1;
	for (; b; b>>=1ll,a=(LL)a*a%M)
		if (b&1) ret=(LL)ret*a%M;
	return ret;
}

int n,m;
int A[maxn],B[maxn];
vector<int> pos[maxn];
multiset<int> S;
multiset<int>::iterator it;
int ans,all;
int main() {
	int i;
	scanf("%d%d",&n,&m);
	FOR(i,1,n) scanf("%d",&A[i]),pos[A[i]].push_back(i);
	FOR(i,1,m) scanf("%d",&B[i]),all+=B[i];
	FOR(i,1,m){
		if (B[i]>(int)pos[i].size()) return 0*puts("-1");
		else B[i]--;
		if (B[i]!=-1) S.insert(pos[i][B[i]]);
	}//puts("OK1");
	ans=INF;
	if (S.empty()) return 0*puts("0");
	FOR(i,1,n){
		int k=A[i];
		if (B[A[i]]==-1) continue;
		it=S.end();it--;
		// printf("%d : %d\n",i,*it);
		ans=min(ans,*it-i-all+1);
		it=S.find(pos[k][B[k]]);
		S.erase(it);
		// printf("erase: %d\n",pos[A[i]][B[A[i]]]);
		B[k]++;
		if (B[k]==(int)pos[k].size()) break;
		S.insert(pos[k][B[k]]);
		// printf("insert: %d\n",pos[A[i]][B[A[i]]]);
	}
	printf("%d\n",ans);
	return 0;
}
/*
8 3
3 3 1 2 2 1 1 3
0 0 1
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
#define dbg(x) cout <<#x<<" = "<<x<<" ;  "
#define dbgln(x) cout <<#x<<" = "<<x<<endl
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=1e6+7;
const double eps=0.00000001;
LL gcd(LL a, LL b) {return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a, T b) {
	T ret=1;
	for (; b; b>>=1ll,a=(LL)a*a%M)
		if (b&1) ret=(LL)ret*a%M;
	return ret;
}

int n,m;
int A[maxn],B[maxn];
vector<int> pos[maxn];
multiset<int> S;
multiset<int>::iterator it;
int ans,all;
int main() {
	int i;
	scanf("%d%d",&n,&m);
	FOR(i,1,n) scanf("%d",&A[i]),pos[A[i]].push_back(i);
	FOR(i,1,m) scanf("%d",&B[i]),all+=B[i];
	FOR(i,1,m){
		if (B[i]>(int)pos[i].size()) return 0*puts("-1");
		else B[i]--;
		S.insert(pos[i][B[i]]);
	}
	ans=INF;
	FOR(i,1,n){
		it=S.end();it--;
		// printf("%d : %d\n",i,*it);
		ans=min(ans,*it-i-all+1);
		it=S.lower_bound(pos[A[i]][B[A[i]]]);
		S.erase(it);
		// printf("erase: %d\n",pos[A[i]][B[A[i]]]);
		B[A[i]]++;
		if (B[A[i]]==(int)pos[A[i]].size()) break;
		S.insert(pos[A[i]][B[A[i]]]);
		// printf("insert: %d\n",pos[A[i]][B[A[i]]]);
	}
	printf("%d\n",ans);
	return 0;
}
/*
8 3
3 3 1 2 2 1 1 3
3 1 1

*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
#include <cassert>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
#define dbg(x) cout <<#x<<" = "<<x<<" ;  "
#define dbgln(x) cout <<#x<<" = "<<x<<endl
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=2e5+7;
const double eps=0.00000001;
LL gcd(LL a, LL b) {return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a, T b) {
	T ret=1;
	for (; b; b>>=1ll,a=(LL)a*a%M)
		if (b&1) ret=(LL)ret*a%M;
	return ret;
}

int n,m;
int A[maxn],B[maxn];
vector<int> pos[maxn];
multiset<int> S;
multiset<int>::iterator it;
int ans,all;
int main() {
	int i;
	scanf("%d%d",&n,&m);
	FOR(i,1,n) scanf("%d",&A[i]),pos[A[i]].push_back(i);
	FOR(i,1,m) scanf("%d",&B[i]),all+=B[i];
	FOR(i,1,m){
		if (B[i]>(int)pos[i].size()) return 0*puts("-1");
		else B[i]--;
		if (B[i]!=-1) S.insert(pos[i][B[i]]);
	}
	ans=INF;
	FOR(i,1,n){
		int k=A[i];
		it=S.end();it--;
		// printf("%d : %d\n",i,*it);
		ans=min(ans,*it-i-all+1);
		it=S.find(pos[k][B[k]]);
		S.erase(it);
		// printf("erase: %d\n",pos[A[i]][B[A[i]]]);
		B[k]++;
		if (B[k]==(int)pos[k].size()) break;
		S.insert(pos[k][B[k]]);
		// printf("insert: %d\n",pos[A[i]][B[A[i]]]);
	}
	printf("%d\n",ans);
	return 0;
}
/*
8 3
3 3 1 2 2 1 1 3
3 1 1

*/


#include<bits/stdc++.h>
using namespace std;
int N,M,A[200000],K[200000],cou[200000],tot;
bool f(int x){
    memset((cou),0,sizeof(cou));
    int satisfy=0,overflow=0;
    for(int i=0;i<tot+x;i++){
        if(cou[A[i]]>=K[A[i]])overflow++;
        cou[A[i]]++;
        if(cou[A[i]]==K[A[i]])satisfy++;
    }
    int l=0;
    int r=tot+x-1;
    while(r<N){
        if(satisfy==M&&overflow<=x)return 1;
        if(cou[A[l]]>K[A[l]])overflow--;
        if(cou[A[l]]==K[A[l]])satisfy--;
        cou[A[l]]--;
        l++;
        r++;
        if(r<N){
            if(cou[A[r]]>=K[A[r]])overflow++;
            cou[A[r]]++;
            if(cou[A[r]]==K[A[r]])satisfy++;
        }
    }
    return 0;
}
int main(){
    scanf("%d %d",&N,&M);
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
        A[i]--;
    }
    tot=0;
    for(int i=0;i<M;i++){
        scanf("%d",&K[i]);
        tot+=K[i];
    }
    int l=0;
    int r=N-tot+1;
    while(l<r){
        int m=(l+r)/2;
        if(!f(m))l=m+1;
        else r=m;
    }
    if(l==N-tot+1)l=-1;
    printf("%d\n",l);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int N,M,A[200000],K[200000],cou[200000],tot;
bool f(int x){
    memset((cou),0,sizeof(cou));
    int satisfy=0,overflow=0;
    for(int i=0;i<M;i++){
        if(K[i]==0)satisfy++;
    }
    for(int i=0;i<tot+x;i++){
        if(cou[A[i]]>=K[A[i]])overflow++;
        cou[A[i]]++;
        if(cou[A[i]]==K[A[i]])satisfy++;
    }
    int l=0;
    int r=tot+x-1;
    while(r<N){
        if(satisfy==M&&overflow<=x)return 1;
        if(cou[A[l]]>K[A[l]])overflow--;
        if(cou[A[l]]==K[A[l]])satisfy--;
        cou[A[l]]--;
        l++;
        r++;
        if(r<N){
            if(cou[A[r]]>=K[A[r]])overflow++;
            cou[A[r]]++;
            if(cou[A[r]]==K[A[r]])satisfy++;
        }
    }
    return 0;
}
int main(){
    scanf("%d %d",&N,&M);
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
        A[i]--;
    }
    tot=0;
    for(int i=0;i<M;i++){
        scanf("%d",&K[i]);
        tot+=K[i];
    }
    int l=0;
    int r=N-tot+1;
    while(l<r){
        int m=(l+r)/2;
        if(!f(m))l=m+1;
        else r=m;
    }
    if(l==N-tot+1)l=-1;
    printf("%d\n",l);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int N,M,A[200000],K[200000],cou[200000],tot;
bool f(int x){
    memset((cou),0,sizeof(cou));
    int satisfy=0,overflow=0;
    for(int i=0;i<tot+x;i++){
        if(cou[A[i]]>=K[A[i]])overflow++;
        cou[A[i]]++;
        if(cou[A[i]]==K[A[i]])satisfy++;
    }
    int l=0;
    int r=tot+x-1;
    while(r<N){
        if(satisfy==M&&overflow<=x)return 1;
        if(cou[A[l]]>K[A[l]])overflow--;
        if(cou[A[l]]==K[A[l]])satisfy--;
        cou[A[l]]--;
        l++;
        r++;
        if(r<N){
            if(cou[A[r]]>=K[A[r]])overflow++;
            cou[A[r]]++;
            if(cou[A[r]]==K[A[r]])satisfy++;
        }
    }
    return 0;
}
int main(){
    scanf("%d %d",&N,&M);
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
        A[i]--;
    }
    tot=0;
    for(int i=0;i<M;i++){
        scanf("%d",&K[i]);
        tot+=K[i];
    }
    int l=0;
    int r=N-tot+1;
    while(l<r){
        int m=(l+r)/2;
        if(!f(m))l=m+1;
        else r=m;
    }
    if(l==N+1)l=-1;
    printf("%d\n",l);
    return 0;
}


#include <iostream>

#include <cstring>

#include <string>
#include <vector>
#include <algorithm>

#include <cassert>

using namespace std;

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

int N, M;
VI C;
VI req;

VI pos[200004];

int count(int c, int from, int to) {
   return upper_bound(pos[c].begin(), pos[c].end(), to) -
          lower_bound(pos[c].begin(), pos[c].end(), from);
}

int solve() {
   int len = accumulate(req.begin(), req.end(), 0);

   FOR(i, 1, N)
      pos[ C[i] ].push_back(i);

   int R = 1;
   FOR(m, 1, M) {
      if (req[m] > SZ(pos[m]))
         return -1;
      int i = pos[m][req[m]-1];
      R = max(R, i);
   }
   //cerr << "R: " << R << endl;

   int res = R - len;

   FOR(L, 2, N) {
      int c_rem = C[L-1];
      int has = count(c_rem, L-1, R);
   //   cerr << "L: " << L << "  has: " << has << endl;
      if (has < req[c_rem]) break;
      if (has == req[c_rem]) {
         int p = upper_bound(pos[c_rem].begin(), pos[c_rem].end(), R) - pos[c_rem].begin();
         ++p;
         if (p >= SZ(pos[c_rem])) break;
         R = max(R, p);
      }
      int cur = R - L + 1 - len;
   //   cerr << "cur: " << cur << endl;
      res = min(res, cur);
   }

   return res;
}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);

   cin >> N >> M;
   C = VI(N+1);
   FOR(i, 1, N)
      cin >> C[i];
   req = VI(M+1);
   FOR(j, 1, M)
      cin >> req[j];

   int res = solve();
   cout << res << endl;

   return 0;
}


#include <iostream>

#include <cstring>

#include <string>
#include <vector>
#include <algorithm>

#include <cassert>

using namespace std;

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

int N, M;
VI C;
VI req;

VI pos[200004];

int count(int c, int from, int to) {
   return upper_bound(pos[c].begin(), pos[c].end(), to) -
          lower_bound(pos[c].begin(), pos[c].end(), from);
}

int solve() {
   int len = accumulate(req.begin(), req.end(), 0);

   FOR(i, 1, N)
      pos[ C[i] ].push_back(i);

   //cerr << "len: " << len << endl;

   int R = 1;
   FOR(m, 1, M) {
      if (req[m] == 0) continue;
      if (req[m] > SZ(pos[m]))
         return -1;
   //   cerr << "m: " << m << endl;
      int i = pos[m][req[m]-1];
      R = max(R, i);
   }
   //cerr << "R: " << R << endl;

   int res = R - len;

   FOR(L, 2, N) {
      int c_rem = C[L-1];

      int start = lower_bound(pos[c_rem].begin(), pos[c_rem].end(), L-1) -
                  pos[c_rem].begin();
      if (start + req[c_rem] >= SZ(pos[c_rem]))
         break;

      R = max(R, pos[c_rem][start + req[c_rem]]);

      int cur = R - L + 1 - len;
   //   cerr << "L: " << L << "  R: " << R << "  cur: " << cur << endl;

   //   cerr << "cur: " << cur << endl;
      res = min(res, cur);
   }

   return res;
}

namespace BF {

int solve() {
   int res = -1;
   FOR(L, 1, N) {
      FOR(R, L, N) {
         VI has(M+1);
         FOR(i, L, R)
            has[ C[i] ]++;
         int to_remove = 0;
         FOR(m, 1, M) {
            if (req[m] > has[m]) {
               to_remove = -1;
               break;
            }
            to_remove += has[m] - req[m];
         }
         if (to_remove >= 0) {
         //   cerr << "L: " << L << "  R: " << R << "  to_remove: " << to_remove << endl;
            if (res < 0)
               res = to_remove;
            else if (res > to_remove)
               res = to_remove;
         }
      }
   }
   return res;
}

}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);

   cin >> N >> M;
   C = VI(N+1);
   FOR(i, 1, N)
      cin >> C[i];

   req = VI(M+1);
   FOR(j, 1, M)
      cin >> req[j];

   int res1 = solve();
   cout << res1 << endl;

//   int res2 = BF::solve();
//   cerr << res1 << endl;
//   cerr << res2 << endl;

   return 0;
}


#include <iostream>

#include <cstring>

#include <string>
#include <vector>
#include <algorithm>

#include <cassert>

using namespace std;

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

int N, M;
VI C;
VI req;

VI pos[200004];

int count(int c, int from, int to) {
   return upper_bound(pos[c].begin(), pos[c].end(), to) -
          lower_bound(pos[c].begin(), pos[c].end(), from);
}

int solve() {
   int len = accumulate(req.begin(), req.end(), 0);

   FOR(i, 1, N)
      pos[ C[i] ].push_back(i);

   //cerr << "len: " << len << endl;

   int R = 1;
   FOR(m, 1, M) {
      if (req[m] == 0) continue;
      if (req[m] > SZ(pos[m]))
         return -1;
   //   cerr << "m: " << m << endl;
      int i = pos[m][req[m]-1];
      R = max(R, i);
   }
   //cerr << "R: " << R << endl;

   int res = R - len;

   FOR(L, 2, N) {
      int c_rem = C[L-1];
      int has = count(c_rem, L-1, R);
   //   cerr << "L: " << L << "  has: " << has << endl;
      if (has < req[c_rem]) break;
      if (has == req[c_rem]) {
         int p = upper_bound(pos[c_rem].begin(), pos[c_rem].end(), R) -
                 pos[c_rem].begin();
         ++p;
         if (p >= SZ(pos[c_rem])) break;
         R = max(R, p);
      }
      int cur = R - L + 1 - len;
   //   cerr << "cur: " << cur << endl;
      res = min(res, cur);
   }

   return res;
}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);

   cin >> N >> M;
   C = VI(N+1);
   FOR(i, 1, N)
      cin >> C[i];

   req = VI(M+1);
   FOR(j, 1, M)
      cin >> req[j];

   int res = solve();
   cout << res << endl;

   return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
inline int read()
{
	int f = 1, x = 0;
	char ch;

	do{
		ch = getchar();
		if (ch == '-')
			f = -1;
	}while(ch < '0' || ch > '9');
	do{
		x = x * 10 + ch - '0';
		ch = getchar();
	}while(ch >= '0' && ch <= '9');
	return f * x;
}
const int N = 2e5;

int n, m, l, r;
int a[N + 1], b[N + 1], sum;
int buc[N + 1], ok;
int ans;

int main()
{
	n = read();
	m = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i <= m; i++) {
		b[i] = read();
		sum += b[i];
		if (!b[i])
			ok++;
	}
	l = 1;
	while (r + 1 <= n) {
		r++;
		buc[a[r]]++;
		if (buc[a[r]] == b[a[r]])
			ok++;
		if (ok == m)
			break;
	}
	if (ok != m) {
		printf("-1\n");
		return 0;
	}
	ans = r - l + 1 - sum;

	while (l <= n && r + 1 <= n) {
		l++;
		if (l > n)
			break;
		if (buc[a[l - 1]] == b[a[l - 1]])
			ok--;
		buc[a[l - 1]]--;
		while (ok < m && r + 1 <= n) {
			r++;
			buc[a[r]]++;
			if (buc[a[r]] == b[a[r]])
				ok++;
		}
		if (ok < m)
			break;
		ans = min(ans, r - l + 1 - sum);
	}

	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
inline int read()
{
	int f = 1, x = 0;
	char ch;

	do{
		ch = getchar();
		if (ch == '-')
			f = -1;
	}while(ch < '0' || ch > '9');
	do{
		x = x * 10 + ch - '0';
		ch = getchar();
	}while(ch >= '0' && ch <= '9');
	return f * x;
}
const int N = 2e5;

int n, m, l, r;
int a[N + 1], b[N + 1], sum;
int buc[N + 1], ok;
int ans;

int main()
{
	n = read();
	m = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i <= m; i++) {
		b[i] = read();
		sum += b[i];
		if (!b[i])
			ok++;
	}
	l = 1;
	while (r + 1 <= n) {
		r++;
		buc[a[r]]++;
		if (buc[a[r]] == b[a[r]])
			ok++;
		if (ok == m)
			break;
	}
	if (ok != m) {
		printf("-1\n");
		return 0;
	}
	ans = r - l + 1 - sum;

	while (l + 1 <= n) {
		l++;
		if (l > n)
			break;
		if (buc[a[l - 1]] == b[a[l - 1]])
			ok--;
		buc[a[l - 1]]--;
		while (ok < m && r + 1 <= n) {
			r++;
			buc[a[r]]++;
			if (buc[a[r]] == b[a[r]])
				ok++;
		}
		if (ok < m)
			break;
		ans = min(ans, r - l + 1 - sum);
	}

	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+5;
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

}


// Problem : F2. Lightsabers (medium)
// Contest : Codeforces - Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)
// URL : https://codeforces.com/problemset/problem/958/F2
// Memory Limit : 256.000000MB
// Time Limit : 1000.000000milisec
// Powered by CP Editor (https://github.com/coder3101/cp-editor2)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+5;
ll n,m,c[N],k[N],cnt[N],ans=1e9,req,cnt_c;
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n>>m;
    for(ll i=0;i<n;i++)cin>>c[i],c[i]--;
    for(ll i=0;i<m;i++)cin>>k[i];
    for(ll i=0;i<m;i++){
        req+=k[i];
        cnt_c+=(k[i]==0);
    }

    for(ll i=0,r=0;i<n;i++){
        while(r<=i || (r<n && cnt_c<m)){
            if(++cnt[c[r]]==k[c[r]])cnt_c++;
            r++;
        }

        if(cnt_c>=m)ans=min(ans,r-i-req);
        if(cnt[c[i]]--==k[c[i]])cnt_c--;
    }

    if(ans==1e9)ans=-1;
    cout<<ans;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

//#include<bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define _USE_DALEN_BRAINS_
#define _USE_MATH_DEFINES_
// You don't know me
#define ll long long
#define ins Not Needed Thing
// The Dalen
#define ull unsigned long long
#define ld long double
// Yeah Yeah
#define Accepted 0
#define pb push_back
// Ora Ora, Ora Ora, Ora fat
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
// Aydin day aydirinday
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
// @Im@5@Im@5 @Im@5@Im@5
#define F first
#define S second
// Skyrim dlya nordov (c) Roflakopter
#define SORRY FUL Accepted
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// Skr Skr v chernih Naykax (c) ZzzzZzzzzZz
#define Toktama Kazakhstan
// TOKTAMA!

using namespace std;

bool isvowel (char c) {
	c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u') return 1;
    return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int PRI = 555557;
const int pri = 997;

int n, m;
int a[N];
int t[N * 4];
int l[N], s1;

void upd (int pos, int x, int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] += x;
		return ;
	}

	int tm = (tl + tr) >> 1;
	if (pos <= tm) upd (pos, x, (v << 1), tl, tm);
	else upd (pos, x, (v << 1) | 1, tm + 1, tr);

	t[v] = max(t[(v << 1)], t[(v << 1) | 1]);
}

inline void Solve () {
	cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
	}

	for (int i = 1; i <= m; i ++) {
		cin >> l[i];
		s1 += l[i];
		upd (i, l[i]);
	}

	for (int i = 1; i <= s1; i ++) {
		upd (a[i], -1);
	}

	if (!t[1]) {
		cout << "YES";
		exit(0);
	}

	for (int i = s1 + 1; i <= n; i ++) {
		upd (a[i - s1], 1);
		upd (a[i], -1);
		if (!t[1]) {
			cout << "YES";
			exit(0);
		}
	}

	cout << "NO";
}

int main () {
	int tests = 1;
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	//cin >> tests;
	while(tests --) {
		Solve ();
		// TooSimple ? True : False
	}
	// Accepted ? True : True
	return Accepted;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

//#include<bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define _USE_DALEN_BRAINS_
#define _USE_MATH_DEFINES_
// You don't know me
#define ll long long
#define ins Not Needed Thing
// The Dalen
#define ull unsigned long long
#define ld long double
// Yeah Yeah
#define Accepted 0
#define pb push_back
// Ora Ora, Ora Ora, Ora fat
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
// Aydin day aydirinday
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
// @Im@5@Im@5 @Im@5@Im@5
#define F first
#define S second
// Skyrim dlya nordov (c) Roflakopter
#define SORRY FUL Accepted
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// Skr Skr v chernih Naykax (c) ZzzzZzzzzZz
#define Toktama Kazakhstan
// TOKTAMA!

using namespace std;

bool isvowel (char c) {
	c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u') return 1;
    return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int PRI = 555557;
const int pri = 997;

int n, m;
int a[N];
int t[N * 4];
int k[N], s1;

void upd (int pos, int x, int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] += x;
		return ;
	}

	int tm = (tl + tr) >> 1;
	if (pos <= tm) upd (pos, x, (v << 1), tl, tm);
	else upd (pos, x, (v << 1) | 1, tm + 1, tr);

	t[v] = max(t[(v << 1)], t[(v << 1) | 1]);
}

void upd1 (int pos, int x, int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] = x;
		return ;
	}

	int tm = (tl + tr) >> 1;
	if (pos <= tm) upd1 (pos, x, (v << 1), tl, tm);
	else upd1 (pos, x, (v << 1) | 1, tm + 1, tr);

	t[v] = max(t[(v << 1)], t[(v << 1) | 1]);
}

bool check (int x) {
	for (int i = 1; i <= m; i ++) {
		upd1 (i, k[i]);
	}

	for (int i = 1; i <= x; i ++)
		upd (a[i], -1);

	if (t[1] <= 0) return 1;

	for (int i = x + 1; i <= n; i ++) {
		upd (a[i], -1);
		upd (a[i - x], 1);
		if (t[1] <= 0) return 1;
	}

	return 0;
}

inline void Solve () {
	cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
	}

	for (int i = 1; i <= m; i ++) {
		cin >> k[i];
		s1 += k[i];
		upd1 (i, k[i]);
	}

	int l = s1, r = n, ans = s1 - 1;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check (mid)) {
			r = mid - 1;
			ans = mid;
		} else l = mid + 1;
	}

	if (ans < s1) {
		cout << -1;
		exit(0);
	}

	cout << ans - s1;
}

int main () {
	int tests = 1;
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	//cin >> tests;
	while(tests --) {
		Solve ();
		// TooSimple ? True : False
	}
	// Accepted ? True : True
	return Accepted;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

//#include<bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define _USE_DALEN_BRAINS_
#define _USE_MATH_DEFINES_
// You don't know me
#define ll long long
#define ins Not Needed Thing
// The Dalen
#define ull unsigned long long
#define ld long double
// Yeah Yeah
#define Accepted 0
#define pb push_back
// Ora Ora, Ora Ora, Ora fat
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
// Aydin day aydirinday
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
// @Im@5@Im@5 @Im@5@Im@5
#define F first
#define S second
// Skyrim dlya nordov (c) Roflakopter
#define SORRY FUL Accepted
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// Skr Skr v chernih Naykax (c) ZzzzZzzzzZz
#define Toktama Kazakhstan
// TOKTAMA!

using namespace std;

bool isvowel (char c) {
	c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u') return 1;
    return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int PRI = 555557;
const int pri = 997;

int n, m;
int a[N];
int t[N * 4];
int k[N], s1;

void upd (int pos, int x, int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] += x;
		return ;
	}

	int tm = (tl + tr) >> 1;
	if (pos <= tm) upd (pos, x, (v << 1), tl, tm);
	else upd (pos, x, (v << 1) | 1, tm + 1, tr);

	t[v] = max(t[(v << 1)], t[(v << 1) | 1]);
}

void upd1 (int pos, int x, int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] = x;
		return ;
	}

	int tm = (tl + tr) >> 1;
	if (pos <= tm) upd1 (pos, x, (v << 1), tl, tm);
	else upd1 (pos, x, (v << 1) | 1, tm + 1, tr);

	t[v] = max(t[(v << 1)], t[(v << 1) | 1]);
}

bool check (int x) {
	for (int i = 1; i <= m; i ++) {
		upd1 (i, k[i]);
	}

	for (int i = 1; i <= x; i ++)
		upd (a[i], -1);

	if (!t[1]) return 1;

	for (int i = x + 1; i <= n; i ++) {
		upd (a[i], -1);
		upd (a[i - x], 1);
		if (!t[1]) return 1;
	}

	return 0;
}

inline void Solve () {
	cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
	}

	for (int i = 1; i <= m; i ++) {
		cin >> k[i];
		s1 += k[i];
		upd1 (i, k[i]);
	}

	int l = s1, r = n, ans = s1 - 1;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check (mid)) {
			r = mid - 1;
			ans = mid;
		} else l = mid + 1;
	}

	if (ans < s1) {
		cout << -1;
		exit(0);
	}

	cout << ans - s1;
}

int main () {
	int tests = 1;
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	//cin >> tests;
	while(tests --) {
		Solve ();
		// TooSimple ? True : False
	}
	// Accepted ? True : True
	return Accepted;
}


/**************************************************************
    Problem: ????
    User: (team ina)   xiaodongc(little_Bear_c)
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:0 kb
    length:0 kb
    score: inf
****************************************************************/
//CLOCKS_PER_SEC
/*
A possible implementation
The first few steps are just BST routine
Node deletion algorithm
Specifically, we're going to have a search
And assume that the target node does exist
So we call the removeAt routine
Remove the node physically
Next, we're still going through a for cycle
Ancestral ancestors traversing deleted nodes
Please pay special attention to our starting point
The father of the deleted node
Instead of inserting it like that
It can start directly from its grandfather
Then, in the course of the whole traversal
Each of us discovers an unbalanced ancestor G
Make a proper rotation adjustment to the ancestor
The three nodes involved in rotation are still G
And the taller kid, P
And the higher grandson V down there
And whether it's unbalanced or rotated
We all need to update the height of this ancestor
You can see that in the worst case
Really need to do logn times
You might as well take the control logic of the previous insert operation
Make a contrast here
No, there's nothing
Tricks to terminate the traversal process ahead of time
Because in the worst case
We really need to leave everything behind
Deal with all generations of ancestors
Since then, we can successfully end the algorithm
And return
So far you may find out
Here's the rotation
We haven't given a specific implementation of it
It's exactly what we suggested
Single spin and double spin in that way?
Interestingly enough, the answer is negative
*/
using namespace std;
#include<bitset>
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<ctime>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mk make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mk(mk(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define titose CLOCKS_PER_SEC
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt");
#define fast ios_base::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
inline long long inc(int &x){return ++x;}
inline long long inc(long long &x){return ++x;}
inline long long inc(int &x,long long y){return x+=y;}
inline long long inc(long long &x,long long y){return x+=y;}
inline double inc(double &x,double y){return x+=y;}
inline long long dec(int &x){return --x;}
inline long long dec(long long &x){return --x;}
inline long long dec(int &x,long long y){return x-=y;}
inline long long dec(long long &x,long long y){return x-=y;}
inline double dec(double &x,double y){return x-=y;}
inline long long mul(int &x){return x=((long long)x)*x;}
inline long long mul(long long &x){return x=x*x;}
inline long long mul(int &x,long long y){return x*=y;}
inline long long mul(long long &x,long long y){return x*=y;}
inline double mul(double &x,double y){return x*=y;}
inline long long divi(int &x){return x=sqrt(x);}
inline long long divi(long long &x){return x=sqrt(x);}
inline long long divi(int &x,long long y){return x/=y;}
inline long long divi(long long &x,long long y){return x/=y;}
inline double divi(double &x,double y){return x/=y;}
inline long long mod(int &x,long long y){return x%=y;}
inline long long mod(long long &x,long long y){return x%=y;}
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define si set<int>
#define sl set<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define msi map<string,int>
#define msl map<string,ll>
#define piii pair<int,pii >
#define piipi pair<pii,int>
#define plll pair<ll,pll >
#define pllpl pair<pll,ll>
#define pqi priority_queue<int>
#define pql priority_queue<ll>
#define npqi priority_queue<int,vector<int>,greater<int> >
#define npql priority_queue<ll,vector<ll>,greater<ll> >
#define forup(i,x,y) if ((x)<=(y)) for ((i)=(x);(i)<=(y);(i)++)
#define fordo(i,x,y) if ((x)>=(y)) for ((i)=(x);(i)>=(y);(i)--)
#define rep(i,x) forup ((i),1,(x))
#define repd(i,x) fordo ((i),(x),1)
#define itr iterator
#define forwyx(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define NO {cout<<"NO";return 0;}
#define YES {cout<<"YES";return 0;}
#define y0 y000000000000000000000000000
#define y1 y111111111111111111111111111
#define j0 j000000000000000000000000000
#define j1 j111111111111111111111111111
#define cl0(a) memset((a),(0),(sizeof((a))))
#define clz(a) memset((a),(0x16),(sizeof((a))))
#define clf(a) memset((a),(-(0x16)),(sizeof((a))))
#define inf 0x3bbbbbbb
#define lnf 0x1bbbbbbbbbbbbbbbll
/*************************************************/
int n,m,i,j,ans=inf,x,a[200005],b[200005],sum,f;
int main()
{
	fast;
	cin>>n>>m;
	rep(i,n)cin>>a[i];
	f=m;
	rep(i,m)
	{
		cin>>b[i];
		sum+=b[i];
		if (b[i]==0) f--;
	}
	rep(i,n)
	{
		while (j<=n&&f>0)
		{
			j++;
			b[a[j]]--;
			if (b[a[j]]==0) f--;
		}
		if (f<=0) ans=min(ans,j-i+1);
		b[a[i]]++;
		if (b[a[i]]==1) f++;
	}
	if (ans>n*2) cout<<-1; else cout<<ans-sum;
	return 0;
}


/**************************************************************
    Problem: ????
    User: (team ina)   xiaodongc(little_Bear_c)
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:0 kb
    length:0 kb
    score: inf
****************************************************************/
//CLOCKS_PER_SEC
/*
A possible implementation
The first few steps are just BST routine
Node deletion algorithm
Specifically, we're going to have a search
And assume that the target node does exist
So we call the removeAt routine
Remove the node physically
Next, we're still going through a for cycle
Ancestral ancestors traversing deleted nodes
Please pay special attention to our starting point
The father of the deleted node
Instead of inserting it like that
It can start directly from its grandfather
Then, in the course of the whole traversal
Each of us discovers an unbalanced ancestor G
Make a proper rotation adjustment to the ancestor
The three nodes involved in rotation are still G
And the taller kid, P
And the higher grandson V down there
And whether it's unbalanced or rotated
We all need to update the height of this ancestor
You can see that in the worst case
Really need to do logn times
You might as well take the control logic of the previous insert operation
Make a contrast here
No, there's nothing
Tricks to terminate the traversal process ahead of time
Because in the worst case
We really need to leave everything behind
Deal with all generations of ancestors
Since then, we can successfully end the algorithm
And return
So far you may find out
Here's the rotation
We haven't given a specific implementation of it
It's exactly what we suggested
Single spin and double spin in that way?
Interestingly enough, the answer is negative
*/
using namespace std;
#include<bitset>
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<ctime>
#include<map>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define ll long long
#define mk make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mk(mk(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define titose CLOCKS_PER_SEC
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt");
#define fast ios_base::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readll(ll &x)
{
	ll v=0ll,f=1ll;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
void writell(ll x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
inline long long inc(int &x){return ++x;}
inline long long inc(long long &x){return ++x;}
inline long long inc(int &x,long long y){return x+=y;}
inline long long inc(long long &x,long long y){return x+=y;}
inline double inc(double &x,double y){return x+=y;}
inline long long dec(int &x){return --x;}
inline long long dec(long long &x){return --x;}
inline long long dec(int &x,long long y){return x-=y;}
inline long long dec(long long &x,long long y){return x-=y;}
inline double dec(double &x,double y){return x-=y;}
inline long long mul(int &x){return x=((long long)x)*x;}
inline long long mul(long long &x){return x=x*x;}
inline long long mul(int &x,long long y){return x*=y;}
inline long long mul(long long &x,long long y){return x*=y;}
inline double mul(double &x,double y){return x*=y;}
inline long long divi(int &x){return x=sqrt(x);}
inline long long divi(long long &x){return x=sqrt(x);}
inline long long divi(int &x,long long y){return x/=y;}
inline long long divi(long long &x,long long y){return x/=y;}
inline double divi(double &x,double y){return x/=y;}
inline long long mod(int &x,long long y){return x%=y;}
inline long long mod(long long &x,long long y){return x%=y;}
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define si set<int>
#define sl set<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define msi map<string,int>
#define msl map<string,ll>
#define piii pair<int,pii >
#define piipi pair<pii,int>
#define plll pair<ll,pll >
#define pllpl pair<pll,ll>
#define pqi priority_queue<int>
#define pql priority_queue<ll>
#define npqi priority_queue<int,vector<int>,greater<int> >
#define npql priority_queue<ll,vector<ll>,greater<ll> >
#define forup(i,x,y) if ((x)<=(y)) for ((i)=(x);(i)<=(y);(i)++)
#define fordo(i,x,y) if ((x)>=(y)) for ((i)=(x);(i)>=(y);(i)--)
#define rep(i,x) forup ((i),1,(x))
#define repd(i,x) fordo ((i),(x),1)
#define itr iterator
#define forwyx(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define NO {cout<<"NO";return 0;}
#define YES {cout<<"YES";return 0;}
#define y0 y000000000000000000000000000
#define y1 y111111111111111111111111111
#define j0 j000000000000000000000000000
#define j1 j111111111111111111111111111
#define cl0(a) memset((a),(0),(sizeof((a))))
#define clz(a) memset((a),(0x16),(sizeof((a))))
#define clf(a) memset((a),(-(0x16)),(sizeof((a))))
#define inf 0x3bbbbbbb
#define lnf 0x1bbbbbbbbbbbbbbbll
/*************************************************/
int n,m,i,j,ans=inf,x,a[200005],b[200005],sum,f;
int main()
{
	fast;
	cin>>n>>m;
	rep(i,n)cin>>a[i];
	rep(i,m)
	{
		cin>>b[i];
		sum+=b[i];
	}
	f=m;
	rep(i,n)
	{
		while (j<=n&&f>0)
		{
			j++;
			b[a[j]]--;
			if (b[a[j]]==0) f--;
		}
		if (f<=0) ans=min(ans,j-i+1);
		b[a[i]]++;
		if (b[a[i]]==1) f++;
	}
	if (ans>n*2) cout<<-1; else cout<<ans-sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[200005],b[200005],num[200005],sum;
int n,m;
bool f;
bool check(int x)
{
    memset(num,0,sizeof(num));
    for(int i=1;i<sum+x;i++)
    {
        num[a[i]]++;
    }
    for(int i=1;i<=n-sum-x+1;i++)
    {
        num[a[i-1]]--;
        num[a[i+sum+x-1]]++;
        f=0;
        for(int j=1;j<=m;j++)
        {
            if(num[j]<b[j])
            {
                break;
            }
            if(j==m)
                f=1;
        }
        if(f)
            break;
    }
    return f;
}
int main()
{
    int L,R,M;
    while(~scanf("%d %d",&n,&m))
    {
        sum=0;
        a[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&b[j]);
            sum+=b[j];
        }
        L=-1;
        R=n-sum;
        while(L+1<R)
        {
            M = (L+R)/2;
            if(check(M))
                R = M;
            else
                L = M;
        }
        if(check(R))
            printf("%d\n",R);
        else
            printf("-1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[200005],b[200005],num[200005],sum;
int n,m;
bool f;
bool check(int x)
{
    memset(num,0,sizeof(num));
    for(int i=1;i<sum+x;i++)
    {
        num[a[i]]++;
    }
    for(int i=1;i<=n-sum-x+1;i++)
    {
        num[a[i-1]]--;
        num[a[i+sum+x-1]]++;
        f=0;
        for(int j=1;j<=m;j++)
        {
            if(num[j]<b[j])
            {
                break;
            }
            if(j==m)
                f=1;
        }
    }
    return f;
}
int main()
{
    int L,R,M;
    while(~scanf("%d %d",&n,&m))
    {
        sum=0;
        a[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&b[j]);
            sum+=b[j];
        }
        L=-1;
        R=n-sum;
        while(L+1<R)
        {
            M = (L+R)/2;
            if(check(M))
                R = M;
            else
                L = M;
        }
        if(check(R))
            printf("%d\n",R);
        else
            printf("-1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[200005],b[200005],num[200005],sum;
int n,m;
bool f;
bool check(int x)
{
    memset(num,0,sizeof(num));
    for(int i=1;i<sum+x;i++)
    {
        num[a[i]]++;
    }
    for(int i=1;i<=n-sum-x+1;i++)
    {
        num[a[i-1]]--;
        num[a[i+sum+x-1]]++;
        f=0;
        for(int j=1;j<=m;j++)
        {
            if(num[j]<b[j])
            {
                break;
            }
            if(j==m)
                f=1;
        }
    }
    return f;
}
int main()
{
    int L,R,M;
    while(~scanf("%d %d",&n,&m))
    {
        sum=0;
        a[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&b[j]);
            sum+=b[j];
        }
        L=-1;
        R=n-sum;
        while(L+1<R)
        {
            M = (L+R)/2;
            if(check(M))
                R = M;
            else
                L = M;
        }
        if(check(R))
            printf("%d\n",R);
        else
            printf("- 1\n");
    }
    return 0;
}


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

const int max_n = 200010;
const int inf   = 1e9+7;

int a[max_n],b[max_n],num[max_n];
int n,m,ans=inf,sum;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
	for(int i=1; i<=m; ++i) scanf("%d",&b[i]),sum+=b[i];
	int tot=m,last=0;
	while(tot && last<n)
	{
		last++;
		num[a[last]]++;
		if(num[a[last]]==b[a[last]]) tot--;
	}
	if(!tot)
	{
		ans=min(ans,last);
		for(int i=1; i<=n; ++i)
		{
			bool flag=true;
			num[a[i]]--;
			if(num[a[i]]<b[a[i]])
			{
				flag=false;
				while(last<n)
				{
				    last++;
					num[a[last]]++;
					if(a[last]==a[i]){flag=true;break;}
				}
			}
			if(!flag) break;
			ans=min(ans,last-i);
		}
	 }
	if(ans==inf) printf("-1\n");
	else printf("%d\n",ans-sum);
	return 0;
}


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

const int max_n = 200010;
const int inf   = 1e9+7;

int a[max_n],b[max_n],num[max_n];
int n,m,ans=inf,sum;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
	for(int i=1; i<=m; ++i) scanf("%d",&b[i]),sum+=b[i];
	int tot=m,last=0;
	for(int i=1; i<=m; ++i)
	  if(b[i]==0) tot--;
	while(tot && last<n)
	{
		last++;
		num[a[last]]++;
		if(num[a[last]]==b[a[last]]) tot--;
	}
	if(!tot)
	{
		ans=min(ans,last);
		for(int i=1; i<=n; ++i)
		{
			bool flag=true;
			num[a[i]]--;
			if(num[a[i]]<b[a[i]])
			{
				flag=false;
				while(last<n)
				{
				    last++;
					num[a[last]]++;
					if(a[last]==a[i]){flag=true;break;}
				}
				if(!flag) break;
			}
			ans=min(ans,last-i);
		}
	 }
	if(ans==inf) printf("-1\n");
	else printf("%d\n",ans-sum);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 202020

int c[N], n, m, k[N];

int cnt[N];

int L, R;

int init_bad, bad;

int s;

void add(int c, int d) {

	if (d == 1) {

		cnt[c] ++;

		if (cnt[c] == k[c]) bad --;

	}

	if (d == -1) {

		if (cnt[c] == k[c]) bad ++;

		cnt[c] --;

	}

}

bool ok(int x) {

	memset(cnt, 0, sizeof cnt);

	L = 1, R = 0;

	bad = init_bad;

	while (R < x + s) R ++, add(c[R], 1);

	if (bad == 0) return 1;

	while (R < n) {

		add(c[L], -1);

		L ++;

		R ++;

		add(c[R], 1);

		if (bad == 0) return 1;

	}

	return 0;

}

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i ++) scanf("%d", &c[i]), cnt[c[i]] ++;

	init_bad = m;

	for (int i = 1; i <= m; i ++) scanf("%d", &k[i]), init_bad -= (k[i] == 0), s += k[i];

	for (int i = 1; i <= m; i ++) if (k[i] > cnt[i]) {

		puts("-1");

		return -1;

	}

	int st = -1, en = n - s, mid;

	while (st < en - 1) {

		mid = (st + en + 1) >> 1;

		if (ok(mid)) en = mid;

		else st = mid;

	}

	printf("%d\n", en);

	return 0;

}


#include <bits/stdc++.h>

using namespace std;

#define N 202020

int c[N], n, m, k[N];

int cnt[N];

int L, R;

int init_bad, bad;

int s;

void add(int c, int d) {

	if (d == 1) {

		cnt[c] ++;

		if (cnt[c] == k[c]) bad --;

	}

	if (d == -1) {

		if (cnt[c] == k[c]) bad ++;

		cnt[c] --;

	}

}

bool ok(int x) {

	memset(cnt, 0, sizeof cnt);

	L = 1, R = 0;

	bad = init_bad;

	while (R < x + s) R ++, add(c[R], 1);

	if (bad == 0) return 1;

	while (R < n) {

		add(c[L], -1);

		L ++;

		R ++;

		add(c[R], 1);

		if (bad == 0) return 1;

	}

	return 0;

}

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i ++) scanf("%d", &c[i]), cnt[c[i]] ++;

	init_bad = m;

	for (int i = 1; i <= m; i ++) scanf("%d", &k[i]), init_bad -= (k[i] == 0), s += k[i];

	for (int i = 1; i <= m; i ++) if (k[i] > cnt[i]) {

		puts("-1");

		return 0;

	}

	int st = -1, en = n - s, mid;

	while (st < en - 1) {

		mid = (st + en + 1) >> 1;

		if (ok(mid)) en = mid;

		else st = mid;

	}

	printf("%d\n", en);

	return 0;

}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 2e5 + 5;
int colors[N];
int target[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d", colors + i);
        colors[i] --;
    }
    rep(i, m) {
        scanf("%d", target + i);
    }
    vector<int> cnt(m, 0);
    int need_removed = 0, meet = 0;
    int answer = n;
    for (int i = 0, j = 0; i < n; i ++) {
        cnt[colors[i]] ++;
        if (cnt[colors[i]] == target[colors[i]]) {
            meet ++;
        } else if (cnt[colors[i]] > target[colors[i]]) {
            need_removed ++;
        }
        while(cnt[colors[j]] - 1 >= target[colors[j]]) {
            cnt[colors[j]] --;
            need_removed --;
            j ++;
        }
        if (meet == m) {
            answer = min(answer, need_removed);
        }
    }
    if (answer == n)    answer = -1;
    printf("%d\n", answer);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 2e5 + 5;
int colors[N];
int target[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d", colors + i);
        colors[i] --;
    }
    rep(i, m) {
        scanf("%d", target + i);
    }
    vector<int> cnt(m, 0);
    int need_removed = 0, meet = 0;
    rep(i, m) {
        meet += target[i] == 0;
    }
    int answer = n;
    for (int i = 0, j = 0; i < n; i ++) {
        cnt[colors[i]] ++;
        if (cnt[colors[i]] == target[colors[i]]) {
            meet ++;
        } else if (cnt[colors[i]] > target[colors[i]]) {
            need_removed ++;
        }
        while(cnt[colors[j]] - 1 >= target[colors[j]]) {
            cnt[colors[j]] --;
            need_removed --;
            j ++;
        }
        if (meet == m) {
            answer = min(answer, need_removed);
        }
    }
    if (answer == n)    answer = -1;
    printf("%d\n", answer);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, k[105], to[105], cnt[105], tot, res, ans, l, r;
int main()
{
#ifdef _noname
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	memset(cnt, 0, sizeof cnt);
	tot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d", k + i);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d", to + i);
		tot += to[i];
	}
	for (int i = 1; i <= tot; i++) cnt[k[i]]++;
	for (int i = 1; i <= m; i++) res += (cnt[i] < to[i]);
	if (!res) ans = 0;
	else ans = -1;
	l = 1, r = tot;
	while (r <= n)
	{
		if (res)
		{
			if (r == n) break;
			r++;
			cnt[k[r]]++;
			if (cnt[k[r]] == to[k[r]]) res--;
		}
		else
		{
			ans = (ans == -1? r - l + 1 - tot : min(ans, r - l + 1 - tot));
			if (cnt[k[l]] == to[k[l]]) res++;
			cnt[k[l]]--;
			l++;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, k[200005], to[200005], cnt[200005], tot, res, ans, l, r;
int main()
{
#ifdef _noname
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	memset(cnt, 0, sizeof cnt);
	tot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d", k + i);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d", to + i);
		tot += to[i];
	}
	for (int i = 1; i <= tot; i++) cnt[k[i]]++;
	for (int i = 1; i <= m; i++) res += (cnt[i] < to[i]);
	if (!res) ans = 0;
	else ans = -1;
	l = 1, r = tot;
	while (r <= n)
	{
		if (res)
		{
			if (r == n) break;
			r++;
			cnt[k[r]]++;
			if (cnt[k[r]] == to[k[r]]) res--;
		}
		else
		{
			ans = (ans == -1? r - l + 1 - tot : min(ans, r - l + 1 - tot));
			if (cnt[k[l]] == to[k[l]]) res++;
			cnt[k[l]]--;
			l++;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    long long n, m, k, cntt=0, res=INT_MAX, sum=0;
    cin >> n >> m;
    long long a[n], b[m+1];

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

    k=0;

    for(int i=0; i<n; i++){
    	while(cntt<m && k<n){
    		b[a[k]]--;
    		if(b[a[k]]==0) cntt++;
    		k++;
    	}
    	if(k==n-1 && cntt<m){
    		break;
    	}
    	if(cntt==m) res=min(res, k-i-sum);
    	b[a[i]]++;
    	if(b[a[i]]==1) cntt--;
    }
    if(res==INT_MAX) cout << -1;
    else cout << res;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    long long n, m, k, cntt=0, res=INT_MAX, sum=0;
    cin >> n >> m;
    long long a[n], b[m+1];

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

    k=0;

    for(int i=0; i<n; i++){
    	while(cntt<m && k<n){
    		b[a[k]]--;
    		if(b[a[k]]==0) cntt++;
    		k++;
    	}
    	if(k==n-1 && cntt<m){
    		break;
    	}
    	if(cntt==m) res=min(res, k-i-sum);
    	b[a[i]]++;
    	if(b[a[i]]==1) cntt--;
    }
    if(res==INT_MAX) cout << -1;
    else cout << res;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    long long n, m, k, cntt=0, res=INT_MAX, sum=0;
    cin >> n >> m;
    long long a[n], b[m+1];

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

    k=0;

    for(int i=0; i<n; i++){
    	while(cntt<m && k<n){
    		b[a[k]]--;
    		if(b[a[k]]==0) cntt++;
    		k++;
    	}
    	if(k==n-1 && cntt<m){
    		cout <<-1; return 0;
    	}
    	if(cntt==m) res=min(res, k-i-sum);
    	b[a[i]]++;
    	if(b[a[i]]==1) cntt--;
    }
    cout << res;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<algorithm>
#include<memory.h>
#include<set>
#include<map>
#include<cmath>
#include<time.h>
#include<iomanip>
#include<time.h>
using namespace std;
typedef long long ll;
#define se second
#define fi first
#define pb push_back
const int N = 1e3;
int n, m;
int a[N], f[N],cur[N];
int ans = 0;
int main() {
	scanf("%d", &n,&m);
	for (int i = 0;i < n;i++)scanf("%d", &a[i]);
	for (int i = 1;i <= m;i++)scanf("%d", &f[i]),ans+=f[i];
	int r = -1;
	int works = 0;
	for (int i = 1;i <= m;i++)works += f[i] == 0;
	int lol = n;
	for (int l = 0;l < n;l++) {
		if (r < l) {
			r = l;
			cur[a[l]]++;
			if (cur[a[l]] == f[a[l]])works++;
		}
		if (l) {
			if (f[a[l - 1]] == cur[a[l - 1]])works--;
			cur[a[l - 1]]--;
		}
		while (works < m && r<n-1) {
			r++;
			cur[a[r]]++;
			if (cur[a[r]] == f[a[r]])works++;
		}
		if (works == m) {
			lol = min(lol, r - l + 1 - ans);
		}
	}
	if (lol == n)puts("-1");
	else printf("%d", lol);

}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<algorithm>
#include<memory.h>
#include<set>
#include<map>
#include<cmath>
#include<time.h>
#include<iomanip>
#include<time.h>
using namespace std;
typedef long long ll;
#define se second
#define fi first
#define pb push_back
const int N = 1e6;
int n, m;
int a[N], f[N],cur[N];
int ans = 0;
int main() {
	scanf("%d%d", &n,&m);
	for (int i = 0;i < n;i++)scanf("%d", &a[i]);
	for (int i = 1;i <= m;i++)scanf("%d", &f[i]),ans+=f[i];
	int r = -1;
	int works = 0;
	for (int i = 1;i <= m;i++)works += f[i] == 0;
	int lol = n;
	for (int l = 0;l < n;l++) {
		if (r < l) {
			r = l;
			cur[a[l]]++;
			if (cur[a[l]] == f[a[l]])works++;
		}
		if (l) {
			if (f[a[l - 1]] == cur[a[l - 1]])works--;
			cur[a[l - 1]]--;
		}
		while (works < m && r<n-1) {
			r++;
			cur[a[r]]++;
			if (cur[a[r]] == f[a[r]])works++;
		}
		if (works == m) {
			lol = min(lol, r - l + 1 - ans);
		}
	}
	if (lol == n)puts("-1");
	else printf("%d", lol);

}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<algorithm>
#include<memory.h>
#include<set>
#include<map>
#include<cmath>
#include<time.h>
#include<iomanip>
#include<time.h>
using namespace std;
typedef long long ll;
#define se second
#define fi first
#define pb push_back
const int N = 1e3;
int n, m;
int a[N], f[N],cur[N];
int ans = 0;
int main() {
	scanf("%d%d", &n,&m);
	for (int i = 0;i < n;i++)scanf("%d", &a[i]);
	for (int i = 1;i <= m;i++)scanf("%d", &f[i]),ans+=f[i];
	int r = -1;
	int works = 0;
	for (int i = 1;i <= m;i++)works += f[i] == 0;
	int lol = n;
	for (int l = 0;l < n;l++) {
		if (r < l) {
			r = l;
			cur[a[l]]++;
			if (cur[a[l]] == f[a[l]])works++;
		}
		if (l) {
			if (f[a[l - 1]] == cur[a[l - 1]])works--;
			cur[a[l - 1]]--;
		}
		while (works < m && r<n-1) {
			r++;
			cur[a[r]]++;
			if (cur[a[r]] == f[a[r]])works++;
		}
		if (works == m) {
			lol = min(lol, r - l + 1 - ans);
		}
	}
	if (lol == n)puts("-1");
	else printf("%d", lol);

}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <utility>

using namespace std;

const int MAXN = 200010;
const int INF = 0x3f3f3f3f;

template<typename T>
T read() {
    T result = 0;int f = 1;char c = getchar();
    while(c > '9' || c < '0') {if(c == '-') f *= -1;c = getchar();}
    while(c <= '9' && c >= '0') {result = result * 10 + c - '0';c = getchar();}
    return result * f;
}

int lazyTag[MAXN],values[MAXN],needValues[MAXN],nowValues[MAXN];

int main() {
    int n = read<int>(),m = read<int>();
    for(int i = 1;i <= n;i++) {
        values[i] = read<int>();
    }
    int result = 0;
    for(int i = 1;i <= m;i++) {
        needValues[i] = read<int>();
        result += needValues[i];
    }
    queue<int> que;
    int nowLazyTagCnt = 0,nowDelete = INF;
    for(int i = 1;i <= n;i++) {
        needValues[values[i]]--;
        result--;
        que.push(i);
        while(needValues[values[i]] < 0) {
            lazyTag[values[i]]++;
            nowLazyTagCnt++;
            needValues[values[i]]++;
            result++;
        }
        while(!que.empty() && lazyTag[values[que.front()]] != 0) {
            int now = que.front();
            que.pop();
            nowLazyTagCnt--;
            lazyTag[values[now]]--;
        }
        if(result == 0) {
            nowDelete = min(nowDelete,nowLazyTagCnt);
        }
    }
    if(result == INF) {
        printf("-1\n");
    }else {
        printf("%d\n",nowDelete);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <utility>

using namespace std;

const int MAXN = 200010;
const int INF = 0x3f3f3f3f;

template<typename T>
T read() {
    T result = 0;int f = 1;char c = getchar();
    while(c > '9' || c < '0') {if(c == '-') f *= -1;c = getchar();}
    while(c <= '9' && c >= '0') {result = result * 10 + c - '0';c = getchar();}
    return result * f;
}

int lazyTag[MAXN],values[MAXN],needValues[MAXN],nowValues[MAXN];

int main() {
    int n = read<int>(),m = read<int>();
    for(int i = 1;i <= n;i++) {
        values[i] = read<int>();
    }
    int result = 0;
    for(int i = 1;i <= m;i++) {
        needValues[i] = read<int>();
        result += needValues[i];
    }
    queue<int> que;
    int nowLazyTagCnt = 0,nowDelete = INF;
    for(int i = 1;i <= n;i++) {
        needValues[values[i]]--;
        result--;
        que.push(i);
        while(needValues[values[i]] < 0) {
            lazyTag[values[i]]++;
            nowLazyTagCnt++;
            needValues[values[i]]++;
            result++;
        }
        while(!que.empty() && lazyTag[values[que.front()]] != 0) {
            int now = que.front();
            que.pop();
            nowLazyTagCnt--;
            lazyTag[values[now]]--;
        }
        if(result == 0) {
            nowDelete = min(nowDelete,nowLazyTagCnt);
        }
    }
    if(nowDelete == INF) {
        printf("-1\n");
    }else {
        printf("%d\n",nowDelete);
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset(a,0,sizeof(a));
#define MEM_1(a) memset(a,-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
int ddx[] = { 0,0,1,1,1,-1,-1,-1 }, ddy[] = { 1,-1,1,0,-1,1,0,-1 };

Vi v[200001];
int a[200001],s[200001];
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	fup(i,1,m,1)v[i].pb(0);
	fup(i,1,n,1)
	{
		scanf("%d",s+i);
		v[s[i]].pb(i);
	}
	int t=0,tot=0;
	fup(i,1,m,1)
	{
		scanf("%d",a+i);
		tot+=a[i];
		if(v[i].size()-1<a[i])return !printf("-1");
		t=max(t,v[i][a[i]]);
	}
	int ans=t-tot;
	fup(i,1,m,1)
	{
		int tt=s[i];
		a[tt]++;
		if(a[tt]>=v[tt].size())break;
		t=max(t,v[tt][a[tt]]);
		ans=min(ans,t-tot-1);
	}
	printf("%d",ans);
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset(a,0,sizeof(a));
#define MEM_1(a) memset(a,-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
int ddx[] = { 0,0,1,1,1,-1,-1,-1 }, ddy[] = { 1,-1,1,0,-1,1,0,-1 };

Vi v[200001];
int a[200001],s[200001];
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	fup(i,1,m,1)v[i].pb(0);
	fup(i,1,n,1)
	{
		scanf("%d",s+i);
		v[s[i]].pb(i);
	}
	int t=0,tot=0;
	fup(i,1,m,1)
	{
		scanf("%d",a+i);
		tot+=a[i];
		if(v[i].size()-1<a[i])return !printf("-1");
		t=max(t,v[i][a[i]]);
	}
	int ans=t-tot;
	fup(i,1,n,1)
	{
		int tt=s[i];
		a[tt]++;
		if(a[tt]>=v[tt].size())break;
		t=max(t,v[tt][a[tt]]);
		ans=min(ans,t-tot-i);
	}
	printf("%d",ans);
}


#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    int n,m,a[200005],b[200005],k=0;
    map <long long,long long> x;
    long long sum=0;
    cin>>n>>m;
    for (int i=0;i<n;i++)
        cin>>a[i];
    for (int i=1;i<=m;i++)
        {cin>>b[i]; sum+=b[i];}
    long long ans=0,ans1=1e18,startpos=0;
    for (int i=0;i<n;i++)
    {
        x[a[i]]++;
        ans++;
        if (x[a[i]]==b[a[i]]) k++;
        if (k==m)
        {
            while (x[a[startpos]]>b[a[startpos]])
            {
                x[a[startpos]]--;
                ans--;
                startpos++;
            }
            ans1=min(ans1,ans);
        }
    }
    cout<<ans1-sum;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    int n,m,a[200005],b[200005],k=0,m1=0;
    map <long long,long long> x;
    long long sum=0;
    cin>>n>>m;
    for (int i=0;i<n;i++)
        cin>>a[i];
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        if (b[i]==0) m1++;
        sum+=b[i];
    }
    m1-=m;
    long long ans=0,ans1=1e18,startpos=0;
    for (int i=0;i<n;i++)
    {
        x[a[i]]++;
        ans++;
        if (x[a[i]]==b[a[i]]) k++;
        if (k>=m)
        {
            while (x[a[startpos]]>b[a[startpos]])
            {
                x[a[startpos]]--;
                ans--;
                startpos++;
            }
            ans1=min(ans1,ans);
        }
    }
    if (ans1==1e18) cout<<-1;
    else cout<<ans1-sum;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    int n,m,a[200005],b[200005],k=0;
    map <long long,long long> x;
    long long sum=0;
    cin>>n>>m;
    for (int i=0;i<n;i++)
        cin>>a[i];
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        if (b[i]==0) m--;
        sum+=b[i];
    }
    long long ans=0,ans1=1e18,startpos=0;
    for (int i=0;i<n;i++)
    {
        x[a[i]]++;
        ans++;
        if (x[a[i]]==b[a[i]]) k++;
        if (k>=m)
        {
            while (x[a[startpos]]>b[a[startpos]])
            {
                x[a[startpos]]--;
                ans--;
                startpos++;
            }
            ans1=min(ans1,ans);
        }
    }
    if (ans1==1e18) cout<<-1;
    else cout<<ans1-sum;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    int n,m,a[200005],b[200005],k=0;
    map <long long,long long> x;
    long long sum=0;
    cin>>n>>m;
    for (int i=0;i<n;i++)
        cin>>a[i];
    for (int i=1;i<=m;i++)
        {cin>>b[i]; sum+=b[i];}
    long long ans=0,ans1=1e18,startpos=0;
    for (int i=0;i<n;i++)
    {
        x[a[i]]++;
        ans++;
        if (x[a[i]]==b[a[i]]) k++;
        if (k>=m)
        {
            while (x[a[startpos]]>b[a[startpos]])
            {
                x[a[startpos]]--;
                ans--;
                startpos++;
            }
            ans1=min(ans1,ans);
        }
    }
    if (ans1==1e18) cout<<-1;
    else cout<<ans1-sum;
    return 0;
}