using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        int m = ReadInt();
        var b = ReadIntArray();
        var a = ReadIntArray();

        int l = 0, r = n;
        while (l < r)
        {
            int mid = (l + r) / 2;
            var f = new bool[m + 1];
            var e = new bool[mid + 1];
            int cnt = 0;
            for (int i = mid; i >= 0; i--)
                if (b[i] > 0 && !f[b[i]])
                {
                    f[b[i]] = true;
                    cnt++;
                    e[i] = true;
                }

            bool ok = cnt == m;
            int v = 0;
            for (int i = 0; i <= mid && ok; i++)
                if (e[i])
                {
                    v -= a[b[i] - 1];
                    if (v < 0)
                        ok = false;
                }
                else
                    v++;

            if (ok)
                r = mid;
            else
                l = mid + 1;
        }

        Write(l < n ? l + 1 : -1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


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

class PairVariable : IComparable<PairVariable>
{
    public long a, b;
    public long c;
    public string val;
    public PairVariable()
    {
        a = b = 0;
        c = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = int.Parse(q[0]);
        b = int.Parse(q[1]);
        c = 0;
    }
    public PairVariable(int a, int b, string x)
    {
        this.a = a;
        this.b = b;
        val = x;
        c = 0;
    }

    public PairVariable(long a, long b, long c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public int CompareTo(PairVariable other)
    {
        return -this.c.CompareTo(other.c);
    }
}

namespace ConsoleApplication3
{
    class Program
    {

        static bool isPrime(int n)
        {
            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
        static int gcd(int a, int b)
        {
            if (b == 0)
            {
                return a;
            }
            else
            {
                return gcd(b, a % b);
            }
        }
        static long factorial(int a)
        {
            long s = 1;
            for (long i = 1; i <= a; i++)
            {
                s *= i;
            }
            return s;
        }

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);
            int m = int.Parse(ss[1]);
            string[] s = Console.ReadLine().Split();
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = int.Parse(s[i]);
            }
            s = Console.ReadLine().Split();
            int[] b = new int[m];
            for (int i = 0; i < m; i++)
            {
                b[i] = int.Parse(s[i]);
            }
            int l = -1;
            int r = n;
            bool ans = false;
            while (l + 1 < r)
            {
                int c = (l + r) / 2;
                //Console.WriteLine(l + " " + r + " " + c);
                Dictionary<int, int> cnt = new Dictionary<int, int>();
                for (int i = 0; i <= c; i++)
                {
                    if (a[i] != 0)
                    {
                        if (!cnt.ContainsKey(a[i]))
                        {
                            cnt.Add(a[i], i);
                        }
                        else
                        {
                            cnt[a[i]] = i;
                        }
                    }
                }

                if (cnt.Count != b.Length)
                {
                    l = c;
                    continue;
                }
                int k = 0;
                bool f = true;
                for (int i = 0; i <= c; i++)
                {
                    //Console.Write(k+ " ");

                    if (a[i] == 0)
                    {
                        k++;
                    }
                    else
                    {
                        if (i == cnt[a[i]])
                        {
                            k -= b[a[i] - 1];
                        }
                        else
                        {
                            k++;
                        }
                    }
                    if (k < 0)
                    {
                        f = false;
                        break;
                    }
                }
                //Console.WriteLine();
                if (f)
                {
                    ans = true;
                    r = c;
                }
                else
                {
                    l = c;
                }
            }
            if (!ans)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(r + 1);
        }
    }
}


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

namespace Competitions.Preparations
{
    public class _732D
    {
        public static int n;
        public static int m;
        public static int minDays;
        public static int[] tasks;
        public static int[] preparations;
        public static void Main(string[] args)
        {
            Console.ReadLine();
            tasks = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            n = tasks.Length;
            preparations = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            m = preparations.Length;

            minDays = preparations.Sum() + m;

            int result = Find();
            Console.WriteLine(result);
        }

        public static int Find()
        {
            int result = -1;
            int a = minDays - 1;
            int b = n-1;
            while (a <= b)
            {
                var t = (a + b)/2;
                if (Solve(t))
                {
                    result = t + 1;
                    b = t - 1;
                }
                else
                    a = t + 1;
            }

            return result;
        }

        public static bool Solve(int pos)
        {
            int cur = (pos + 1) - minDays;
            int i = pos;
            int cnt = 0;
            var mark = new bool[preparations.Length];
            for (; i >= 0 && cur >= 0; i--, cur--)
            {
                if (tasks[i] == 0 || mark[tasks[i]-1]) continue;
                cnt++;
                mark[tasks[i]-1] = true;
                cur += preparations[tasks[i]-1]+1;
            }

            return cnt == m;
        }
    }
}


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

class Program
{
    int[] d, a;
    bool[] b;
    int m;

    // true - двигается от from к to
    bool Check(int val)
    {
        for (int i = 0; i < m; i++)
            b[i] = false;
        long sum = 0;
        for (int i = val; i >= 0; i--)
        {
            if (d[i] != -1 && !b[d[i]])
            {
                b[d[i]] = true;
                sum += a[d[i]];
            }
            else
            {
                if (sum != 0)
                    sum--;
            }
        }
        for (int i = 0; i < m; i++)
            if (b[i] == false)
                return true;
        return sum != 0;
    }

    //http://codeforces.com/contest/725/problem/D
    //http://codeforces.com/contest/732/problem/D
    // to>=from
    // Check(from)=true
    // tttfff
    int BS(int from, int to)
    {
        int res = from - 1;
        for (;;)
        {
            if (from > to)
            {
                return res;
            }
            var mid = (from + to) / 2;
            if (Check(mid))
            {
                res = mid;
                from = mid + 1;
            }
            else
            {
                to = mid - 1;
            }
        }
    }

    //////////////////////////////////////////////////
    void Solution()
    {
        var n = ri;
        m = ri;
        d = ria(n);
        a = ria(m);
        b = new bool[m];
        for (int i = 0; i < n; i++)
            d[i]--;

        var q = BS(0, n - 1) + 1;
        if (q == n)
        {
            wln(-1);
            return;
        }
        wln(q + 1);
    }
    //////////////////////////////////////////////////

    static void swap<T>(ref T o1, ref T o2) { var o = o1; o1 = o2; o2 = o; }

    static void wln() { Console.WriteLine(); }
    static void wln<T>(T o) { Console.WriteLine(o); }
    static void wsp<T>(T o) { Console.Write(o + " "); }
    static void wrt<T>(T o) { Console.Write(o); }

    static void wln(double o) { Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }
    static void wsp(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12} ", o)); }
    static void wrt(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }

    static long gcd(long a, long b) { return (b != 0) ? gcd(b, a % b) : a; }
    static long lcm(long a, long b) { return a / gcd(a, b) * b; }

    int ri
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            int p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    long rl
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    double rd
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind; int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0, q = 1; while (_ind < _count && _str[_ind] != ' ' && _str[_ind] != '.') p = p * 10 + _str[_ind++] - '0';
            if (_ind < _count && _str[_ind] == '.') { ++_ind; while (_ind < _count && _str[_ind] != ' ') { p = p * 10 + _str[_ind++] - '0'; q *= 10; } }
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return (double)p / q * sign;
        }
    }
    string rs
    {
        get
        {
            _read(); while (_ind < _count && _str[_ind] == ' ') ++_ind;
            var tmp = _str.IndexOf(' ', _ind);
            if (tmp == -1) { _count = 0; _ind = 0; return _str.Substring(_ind); }
            var s = _str.Substring(_ind, tmp - _ind); _ind = tmp + 1;
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return s;
        }
    }

    int[] ria(int n)
    {
        var res = new int[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                int p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    long[] rla(int n)
    {
        var res = new long[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    double[] rda(int n)
    {
        var res = new double[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0, q = 1; while (i < m && s[i] != ' ' && s[i] != '.') p = p * 10 + s[i++] - '0';
                if (i < m && s[i] == '.') { ++i; while (i < m && s[i] != ' ') { p = p * 10 + s[i++] - '0'; q *= 10; } }
                res[k++] = (double)p / q * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    string[] rsa(int n) { var res = new string[n]; for (int k = 0; k < n; ++k) res[k] = Console.ReadLine(); _count = 0; _ind = 0; return res; }
    string _str = null; int _count = 0; int _ind = 0;
    void _read() { if (_ind == _count) { _str = Console.ReadLine(); _count = _str.Length; _ind = 0; } }
    static void Main() { new Program().Solution(); }
}


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

class Program
{
    int[] d, a;
    bool[] b;
    int m;

    // true - двигается от from к to
    bool Check(int val)
    {
        for (int i = 0; i < m; i++)
            b[i] = false;
        long sum = 0;
        for (int i = val; i >= 0; i--)
        {
            if (d[i] != -1 && !b[d[i]])
            {
                b[d[i]] = true;
                sum += a[d[i]];
            }
            else
            {
                if (sum != 0)
                    sum--;
            }
        }
        return sum != 0;
    }

    //http://codeforces.com/contest/725/problem/D
    //http://codeforces.com/contest/732/problem/D
    // to>=from
    // Check(from)=true
    // tttfff
    int BS(int from, int to)
    {
        int res = from - 1;
        for (;;)
        {
            if (from > to)
            {
                return res;
            }
            var mid = (from + to) / 2;
            if (Check(mid))
            {
                res = mid;
                from = mid + 1;
            }
            else
            {
                to = mid - 1;
            }
        }
    }

    //////////////////////////////////////////////////
    void Solution()
    {
        var n = ri;
        m = ri;
        d = ria(n);
        a = ria(m);
        b = new bool[m];
        for (int i = 0; i < n; i++)
            d[i]--;

        var q = BS(0, n - 1) + 1;
        if (q == n)
        {
            wln(-1);
            return;
        }
        wln(q + 1);
    }
    //////////////////////////////////////////////////

    static void swap<T>(ref T o1, ref T o2) { var o = o1; o1 = o2; o2 = o; }

    static void wln() { Console.WriteLine(); }
    static void wln<T>(T o) { Console.WriteLine(o); }
    static void wsp<T>(T o) { Console.Write(o + " "); }
    static void wrt<T>(T o) { Console.Write(o); }

    static void wln(double o) { Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }
    static void wsp(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12} ", o)); }
    static void wrt(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }

    static long gcd(long a, long b) { return (b != 0) ? gcd(b, a % b) : a; }
    static long lcm(long a, long b) { return a / gcd(a, b) * b; }

    int ri
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            int p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    long rl
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    double rd
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind; int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0, q = 1; while (_ind < _count && _str[_ind] != ' ' && _str[_ind] != '.') p = p * 10 + _str[_ind++] - '0';
            if (_ind < _count && _str[_ind] == '.') { ++_ind; while (_ind < _count && _str[_ind] != ' ') { p = p * 10 + _str[_ind++] - '0'; q *= 10; } }
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return (double)p / q * sign;
        }
    }
    string rs
    {
        get
        {
            _read(); while (_ind < _count && _str[_ind] == ' ') ++_ind;
            var tmp = _str.IndexOf(' ', _ind);
            if (tmp == -1) { _count = 0; _ind = 0; return _str.Substring(_ind); }
            var s = _str.Substring(_ind, tmp - _ind); _ind = tmp + 1;
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return s;
        }
    }

    int[] ria(int n)
    {
        var res = new int[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                int p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    long[] rla(int n)
    {
        var res = new long[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    double[] rda(int n)
    {
        var res = new double[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0, q = 1; while (i < m && s[i] != ' ' && s[i] != '.') p = p * 10 + s[i++] - '0';
                if (i < m && s[i] == '.') { ++i; while (i < m && s[i] != ' ') { p = p * 10 + s[i++] - '0'; q *= 10; } }
                res[k++] = (double)p / q * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    string[] rsa(int n) { var res = new string[n]; for (int k = 0; k < n; ++k) res[k] = Console.ReadLine(); _count = 0; _ind = 0; return res; }
    string _str = null; int _count = 0; int _ind = 0;
    void _read() { if (_ind == _count) { _str = Console.ReadLine(); _count = _str.Length; _ind = 0; } }
    static void Main() { new Program().Solution(); }
}


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

class Program
{
    int[] d, a;
    bool[] b;
    int m;

    // true - двигается от from к to
    bool Check(int val)
    {
        for (int i = 0; i < m; i++)
            b[i] = false;
        int sum = 0;
        for (int i = val; i >= 0; i--)
        {
            if (d[i] != -1 && !b[d[i]])
            {
                b[d[i]] = true;
                sum += a[d[i]];
            }
            else
            {
                if (sum != 0)
                    sum--;
            }
        }
        return sum != 0;
    }

    //http://codeforces.com/contest/725/problem/D
    //http://codeforces.com/contest/732/problem/D
    // to>=from
    // Check(from)=true
    // tttfff
    int BS(int from, int to)
    {
        int res = from - 1;
        for (;;)
        {
            if (from > to)
            {
                return res;
            }
            var mid = (from + to) / 2;
            if (Check(mid))
            {
                res = mid;
                from = mid + 1;
            }
            else
            {
                to = mid - 1;
            }
        }
    }

    //////////////////////////////////////////////////
    void Solution()
    {
        var n = ri;
        m = ri;
        d = ria(n);
        a = ria(m);
        b = new bool[m];
        for (int i = 0; i < n; i++)
            d[i]--;

        var q = BS(0, n - 1) + 1;
        if (q == n)
        {
            wln(-1);
            return;
        }
        wln(q + 1);
    }
    //////////////////////////////////////////////////

    static void swap<T>(ref T o1, ref T o2) { var o = o1; o1 = o2; o2 = o; }

    static void wln() { Console.WriteLine(); }
    static void wln<T>(T o) { Console.WriteLine(o); }
    static void wsp<T>(T o) { Console.Write(o + " "); }
    static void wrt<T>(T o) { Console.Write(o); }

    static void wln(double o) { Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }
    static void wsp(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12} ", o)); }
    static void wrt(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }

    static long gcd(long a, long b) { return (b != 0) ? gcd(b, a % b) : a; }
    static long lcm(long a, long b) { return a / gcd(a, b) * b; }

    int ri
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            int p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    long rl
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    double rd
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind; int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0, q = 1; while (_ind < _count && _str[_ind] != ' ' && _str[_ind] != '.') p = p * 10 + _str[_ind++] - '0';
            if (_ind < _count && _str[_ind] == '.') { ++_ind; while (_ind < _count && _str[_ind] != ' ') { p = p * 10 + _str[_ind++] - '0'; q *= 10; } }
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return (double)p / q * sign;
        }
    }
    string rs
    {
        get
        {
            _read(); while (_ind < _count && _str[_ind] == ' ') ++_ind;
            var tmp = _str.IndexOf(' ', _ind);
            if (tmp == -1) { _count = 0; _ind = 0; return _str.Substring(_ind); }
            var s = _str.Substring(_ind, tmp - _ind); _ind = tmp + 1;
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return s;
        }
    }

    int[] ria(int n)
    {
        var res = new int[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                int p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    long[] rla(int n)
    {
        var res = new long[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    double[] rda(int n)
    {
        var res = new double[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0, q = 1; while (i < m && s[i] != ' ' && s[i] != '.') p = p * 10 + s[i++] - '0';
                if (i < m && s[i] == '.') { ++i; while (i < m && s[i] != ' ') { p = p * 10 + s[i++] - '0'; q *= 10; } }
                res[k++] = (double)p / q * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    string[] rsa(int n) { var res = new string[n]; for (int k = 0; k < n; ++k) res[k] = Console.ReadLine(); _count = 0; _ind = 0; return res; }
    string _str = null; int _count = 0; int _ind = 0;
    void _read() { if (_ind == _count) { _str = Console.ReadLine(); _count = _str.Length; _ind = 0; } }
    static void Main() { new Program().Solution(); }
}


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

class Program
{
    private static bool Can(int n, int[] a, int[] b)
    {
        for (int i = 0; i < pos.Length; i++) pos[i] = -1;
        for (int i = 0; i < n; i++)
        {
            pos[a[i]] = i;
        }

        pos[0] = 0;
        if (pos.Any(q => q == -1)) return false;

        int count = 0;
        for (int i = 0; i < n; i++)
            if (a[i] > 0 && pos[a[i]] == i)
            {
                if (b[a[i] - 1] > count) return false;
                count -= b[a[i] - 1];
            }
            else
                count++;

        return true;
    }

    private static int[] pos;
    static void Main(string[] args)
    {
        int n = RI();
        int m = RI();
        var a = RIA(n);
        var b = RIA(m);
        pos = new int[m + 1];

        if (!Can(n, a, b))
        {
            Console.WriteLine(-1);
            return;
        }

        int l = 0;
        int r = n;
        while (r - l > 1)
        {
            int mid = (l + r) / 2;
            if (Can(mid, a, b))
                r = mid;
            else
                l = mid;
        }

        Console.WriteLine(r);
    }

    private const int Mod = 1000000000 + 7;

    #region Data Read
    private static char ReadSign()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if (ans == '+' || ans == '-' || ans == '?')
                return (char)ans;
        }
    }

    private static long GCD(long a, long b)
    {
        if (a % b == 0) return b;
        return GCD(b, a % b);
    }

    private static List<int>[] ReadGraph(int n, int m)
    {
        List<int>[] g = new List<int>[n];
        for (int i = 0; i < g.Length; i++) g[i] = new List<int>();
        for (int i = 0; i < m; i++)
        {
            int a = RI() - 1;
            int b = RI() - 1;

            g[a].Add(b);
            g[b].Add(a);
        }

        return g;
    }

    private static int[,] ReadGraphAsMatrix(int n, int m)
    {
        int[,] matrix = new int[n + 1, n + 1];
        for (int i = 0; i < m; i++)
        {
            int a = RI();
            int b = RI();
            matrix[a, b] = matrix[b, a] = 1;
        }

        return matrix;
    }

    private static void Sort(ref int a, ref int b)
    {
        if (a > b) Swap(ref a, ref b);
    }

    private static void Swap(ref int a, ref int b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }

    private const int BufferSize = 1 << 16;
    private static StreamReader consoleReader;
    private static MemoryStream testData;

    private static int RI()
    {
        int ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static ulong RUL()
    {
        ulong ans = 0;
        int chr;
        do
        {
            chr = consoleReader.Read();
            if (chr == -1)
                return 0;
        } while (chr < '0' || chr > '9');

        ans = (uint)(chr - '0');
        while (true)
        {
            chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans;
            ans = ans * 10 + (uint)(chr - '0');
        }
    }

    private static long RL()
    {
        long ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static int[] RIA(int n)
    {
        int[] ans = new int[n];
        for (int i = 0; i < n; i++)
            ans[i] = RI();
        return ans;
    }

    private static long[] RLA(int n)
    {
        long[] ans = new long[n];
        for (int i = 0; i < n; i++)
            ans[i] = RL();
        return ans;
    }

    private static char[] ReadWord()
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (!((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z')));

        List<char> s = new List<char>();
        do
        {
            s.Add((char)ans);
            ans = consoleReader.Read();
        } while ((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z'));

        return s.ToArray();
    }

    private static char[] ReadString(int n)
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (!((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z')));

        char[] s = new char[n];
        int pos = 0;
        do
        {
            s[pos++] = (char)ans;
            if (pos == n) break;
            ans = consoleReader.Read();
        } while ((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z'));

        return s;
    }

    private static char[] ReadStringLine()
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (ans == 10 || ans == 13);

        List<char> s = new List<char>();

        do
        {
            s.Add((char)ans);
            ans = consoleReader.Read();
        } while (ans != 10 && ans != 13);

        return s.ToArray();
    }

    private static char ReadLetter()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if ((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z'))
                return (char)ans;
        }
    }
    private static char ReadNonLetter()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if (!((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z')))
                return (char)ans;
        }
    }

    private static char ReadAnyOf(IEnumerable<char> allowed)
    {
        while (true)
        {
            char ans = (char)consoleReader.Read();
            if (allowed.Contains(ans))
                return ans;
        }
    }

    private static char ReadDigit()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if (ans >= '0' && ans <= '9')
                return (char)ans;
        }
    }

    private static int ReadDigitInt()
    {
        return ReadDigit() - (int)'0';
    }

    private static char ReadAnyChar()
    {
        return (char)consoleReader.Read();
    }

    private static string DoubleToString(double x)
    {
        return x.ToString(CultureInfo.InvariantCulture);
    }

    private static double DoubleFromString(string x)
    {
        return double.Parse(x, CultureInfo.InvariantCulture);
    }

    static Program()
    {
        //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        consoleReader = new StreamReader(Console.OpenStandardInput(BufferSize), Encoding.ASCII, false, BufferSize);
    }

    private static void PushTestData(StringBuilder sb)
    {
        PushTestData(sb.ToString());
    }
    private static void PushTestData(string data)
    {
#if TOLYAN_TEST
        if (testData == null)
        {
            testData = new MemoryStream();
            consoleReader = new StreamReader(testData);
        }

        var pos = testData.Position;
        var bytes = Encoding.UTF8.GetBytes(data);
        testData.Write(bytes, 0, bytes.Length);
        testData.Flush();
        testData.Position = pos;
#endif
    }
    #endregion
}


import java.io.*
import java.util.*
import java.lang.Math.*
import java.util.stream.IntStream
import kotlin.comparisons.compareBy

class Task {
    internal var `in`: FastScanner = FastScanner(System.`in`)
    internal var out: PrintWriter = PrintWriter(System.out)

    internal inner class Tuple(var a: Int, var b: Int, var c: Int)

    var n: Int = 0
    var m: Int = 0
    lateinit var d: IntArray
    lateinit var days: IntArray
    lateinit var used: BooleanArray

    fun can(v: Int): Boolean {
        Arrays.fill(used, false)
        var cnt = 0
        val list = ArrayList<Tuple>()
        val indexes = ArrayList<Int>()
        for (i in v downTo 0) {
            if (d[i] < 0)
                continue
            if (used[d[i]])
                continue
            used[d[i]] = true
            cnt++
            indexes.add(i)
            list.add(Tuple(i - days[d[i]], days[d[i]], i))
        }
        Collections.reverse(indexes)
        if (cnt < m)
            return false
        Collections.sort(list) { t1, t2 ->
            if (t1.a === t2.a) {
                if (t1.b === t2.b)
                    return@sort t1.c.compareTo(t2.c)
                return@sort t1.b.compareTo(t2.b)
            }
            return@sort t1.a.compareTo(t2.a)
        }

        var time: Long = 0
        var cur = 0
        for (tuple in list) {
            time += tuple.b.toLong()
            while (cur < indexes.size && time - 1 >= indexes.get(cur)) {
                cur++
                time++
            }
            if (time > tuple.c)
                return false
        }

        return true
    }

    fun solve() {
        n = `in`.nextInt()
        m = `in`.nextInt()
        d = IntArray(n)
        used = BooleanArray(m)
        for (i in 0..n - 1)
            d[i] = `in`.nextInt() - 1
        days = IntArray(m)
        for (i in 0..m - 1)
            days[i] = `in`.nextInt()

        var a = 0
        var b = n - 1
        while (a != b) {
            val la = a
            val lb = b
            val mid = (a + b) / 2
            if (can(mid))
                b = mid
            else
                a = mid
            if (la == a && lb == b) {
                if (can(a))
                    b = a
                else
                    a = b
            }
        }

        if (!can(a))
            out.print(-1)
        else
            out.print(a + 1)
    }

    fun run() {
        solve()
        out.close()
    }

    internal inner class FastScanner(`is`: InputStream) {
        var br: BufferedReader
        var st: StringTokenizer? = null

        init {
            br = BufferedReader(InputStreamReader(`is`))
        }

        operator fun next(): String {
            while (st == null || !st!!.hasMoreTokens()) {
                try {
                    st = StringTokenizer(br.readLine())
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
            return st!!.nextToken()
        }

        fun nextLine(): String? {
            st = null
            try {
                return br.readLine()
            } catch (e: IOException) {
                e.printStackTrace()
                return null
            }

        }

        fun nextInt(): Int {
            return Integer.parseInt(next())
        }

        fun nextLong(): Long {
            return java.lang.Long.parseLong(next())
        }

        fun nextDouble(): Double {
            return java.lang.Double.parseDouble(next())
        }

        fun nextFloat(): Float {
            return java.lang.Float.parseFloat(next())
        }
    }

    companion object {
        internal val FILE = ""
    }

}

fun main(args: Array<String>) {
    Task().run()
}


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

int *day;

struct prob{
  int p,need,last;
} *probs;

int n,m;

int bisearch(int l,int h);
bool check(int last);
int comp(const void *p1,const void *p2);
void impossible(void);

int main(void){
  int i,j,k;
  int minsum=0;
  scanf("%d%d",&n,&m);
  day=(int *)calloc(n+1,sizeof(int));
  probs=(struct prob *)calloc(m,sizeof(struct prob));

  if(n<m){
      impossible();
      return 0;
  }

  for(i=1;i<=n;i++) scanf("%d",day+i);
  for(i=0;i<m;i++){
    scanf("%d",&probs[i].need);
    probs[i].p=i;
    minsum+=probs[i].need+1;
    if(minsum>n){
      impossible();
      return 0;
    }
  }

  if(!check(n)){
    impossible();
    return 0;
  }
  k=check(minsum)?minsum:bisearch(minsum,n);
  printf("%d\n",k);
  free(day);
  free(probs);
  return 0;
}

int bisearch(int l,int h){
  int last=(l+h)/2;
  if(l==last) return h;
  if(check(last)) return bisearch(l,last);
  else return bisearch(last,h);
}

bool check(int last){
  int i,j,k;
  struct prob *copy;
  int now=0,minus=0;

  copy=(struct prob *)calloc(m,sizeof(struct prob));
  for(i=0;i<m;i++) copy[i]=probs[i];

  for(i=last;i>0;i--){
    if(copy[day[i]-1].last==0) copy[day[i]-1].last=i;
  }

  for(i=0;i<m;i++){
    if(copy[i].last==0){
      free(copy);
      return false;
    }
  }

  qsort(copy,m,sizeof(struct prob),comp);

  for(i=0;i<m;i++){
    now=copy[i].last;
    minus+=copy[i].need+1;
    if(now<minus){
      free(copy);
      return false;
    }
  }
  free(copy);
  return true;
}

int comp(const void *p1,const void *p2){
  struct prob st1=*(const struct prob *)p1;
  struct prob st2=*(const struct prob *)p2;
  return st1.last-st2.last;
}

void impossible(void){
  puts("-1");
  free(day);
  free(probs);
}


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

int *day;

struct prob{
  int p,need,last;
} *probs;

int n,m;

int bisearch(int l,int h);
bool check(int last);
int comp(const void *p1,const void *p2);
void impossible(void);

int main(void){
  int i,j,k;
  int minsum=0;
  scanf("%d%d",&n,&m);
  day=(int *)calloc(n+1,sizeof(int));
  probs=(struct prob *)calloc(m,sizeof(struct prob));

  for(i=1;i<=n;i++) scanf("%d",day+i);
  for(i=0;i<m;i++){
    scanf("%d",&probs[i].need);
    probs[i].p=i;
    minsum+=probs[i].need+1;
    if(minsum>n){
      impossible();
      return 0;
    }
  }

  if(!check(n)){
    impossible();
    return 0;
  }
  k=check(minsum)?minsum:bisearch(minsum,n);
  printf("%d\n",k);
  free(day);
  free(probs);
  return 0;
}

int bisearch(int l,int h){
  int last=(l+h)/2;
  if(l==h) return h;
  if(check(last)) return bisearch(l,last-1);
  else return bisearch(last+1,h);
}

bool check(int last){
  int i,j,k;
  struct prob *copy;
  int now=0,minus=0;

  copy=(struct prob *)calloc(m,sizeof(struct prob));
  for(i=0;i<m;i++) copy[i]=probs[i];

  for(i=last;i>0;i--){
    if(copy[day[i]].last==0) copy[day[i]].last=i;
  }

  qsort(copy,m,sizeof(struct prob),comp);

  for(i=0;i<m;i++){
    now=copy[i].last;
    minus+=copy[i].need+1;
    if(now<minus){
      free(copy);
      return false;
    }
  }
  free(copy);
  return true;
}

int comp(const void *p1,const void *p2){
  struct prob st1=*(const struct prob *)p1;
  struct prob st2=*(const struct prob *)p2;
  return st1.last-st2.last;
}

void impossible(void){
  puts("-1");
  free(day);
  free(probs);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include<ctype.h>
#include<limits.h>
#define eps 1e-9
#define ll long long int
#define N 500500
struct node{
   int x,y;
}solve[N];
int arr[N];
int foo[N];
int used[N];
int hm[N];
int cmp(const void *a,const void *b){
   struct node *x=(struct node*)a;
   struct node *y=(struct node*)b;
   return x->x - y->x;
}
int ok(int mid,int x){
   int k=0;
   for(int i=mid;i>=1;i--){
      if(!used[arr[i]] && arr[i]!=0){
         solve[k].x=i;
         solve[k].y=arr[i];
         used[arr[i]]=1;
         k++;
      }
   }
   memset(used,0,sizeof(used));
   qsort(solve,k,sizeof(struct node),cmp);
   int rem=0; int curr=0; int add=0;
   for(int i=0;i<k;i++){
      int res=solve[i].x-1-foo[solve[i].y]-rem+add;
      add=res;
      if(res<0)
      return -1;
      rem=solve[i].x;
   }
   return k==x?1:-1;
}
int main(){
   int a,b,c;
   scanf("%d%d",&a,&b);
   for(int w=1;w<=a;w++){
      scanf("%d",arr+w);
   }
   for(int i=1;i<=b;i++){
      scanf("%d",foo+i);
   }
   int lb=1; int ub=a; int mid;
   while(lb<=ub){
      int mid=(lb+ub)/2;
      if(ok(mid,b)!=-1)
      ub=mid-1;
      else lb=mid+1;
   }
   (ub+1)>a?printf("-1"):printf("%d",ub+1);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include<ctype.h>
#include<limits.h>
#define eps 1e-9
#define ll long long int
#define N 500500
struct node{
   int subject;
   int time;
}get[N];
int arr[N];
int done[N];
int cmp(const void *a,const void *b){
   struct node *x=(struct node*)a;
   struct node *y=(struct node*)b;
   return y->time - x->time;
}
int find(int arr[],int target,int n){
   int l=0; int r=n-1;
   while(l<=r){
      int mid=(l+r)/2;
      if(target==arr[mid])
      return mid;
      if(target>arr[mid]){
         l=mid+1;
      }
      else r=mid-1;
   }
   return -1;
}
int main(){
   int a,b,c;
   scanf("%d%d",&a,&b);
   for(int w=0;w<a;w++){
      scanf("%d",arr+w);
   }
   for(int w=0;w<b;w++){
      scanf("%d",&get[w].time);
      get[w].subject=w+1;
   }
   qsort(get,b,sizeof(struct node),cmp); int curr=0;int k=0; int cnt=0; int bar=0;
   for(int w=0;w<a;w++){
      int foo=find(done,arr[w],k);
      if(foo!=-1 && arr[w]!=0){
         done[foo]=-1;
         bar++;
         cnt=w+1;
         continue;
      }
      get[curr].time--;
      if(get[curr].time == 0){
         done[k++]=get[curr].subject;
         curr++;
      }
   }
   if(bar!=b){
      printf("-1");
   }
   else printf("%d",cnt);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include<ctype.h>
#include<limits.h>
#define eps 1e-9
#define ll long long int
#define N 500500
struct node{
   int x,y;
}solve[N];
int arr[N];
int foo[N];
int used[N];
int hm[N];
int cmp(const void *a,const void *b){
   struct node *x=(struct node*)a;
   struct node *y=(struct node*)b;
   return x->x - y->x;
}
int ok(int mid,int x){
   int k=0;
   for(int i=mid;i>=1;i--){
      if(!used[arr[i]] && arr[i]!=0){
         solve[k].x=i;
         solve[k].y=arr[i];
         used[arr[i]]=1;
         k++;
      }
   }
   memset(used,0,sizeof(used));
   qsort(solve,k,sizeof(struct node),cmp);
   int rem=0; int curr=0; int add=0;
   for(int i=0;i<k;i++){
      int res=solve[i].x-1-foo[solve[i].y]-rem+add;
      add=res;
      if(res<0)
      return -1;
      rem=solve[i].x;
   }
   return k==x?1:-1;
}
int main(){
   int a,b,c;
   scanf("%d%d",&a,&b);
   for(int w=1;w<=a;w++){
      scanf("%d",arr+w);
   }
   for(int i=1;i<=b;i++){
      scanf("%d",foo+i);
   }
   int lb=1; int ub=a; int mid;
   while(lb<=ub){
      int mid=(lb+ub)/2;
      if(ok(mid,b)!=-1)
      ub=mid-1;
      else lb=mid+1;
   }
   (ub+1)>=a?printf("-1"):printf("%d",ub+1);
}


#ifdef ONLINE_JUDGE
    #define NDEBUG 1
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>

#define long int64_t
#define fore(i,k,n) for (int _k = (k), _n = (n), i = _k; i <= _n; ++i)
#define forr(i,n,k) for (int _k = (k), _n = (n), i = _n; i >= _k; --i)
#define LIST_INIT_CAP 4
#define HASH_INIT_CAP 64
#define HASH_SKIP 11
#define TREE_INIT_CAP 1024
#define TREE_MAX_HEIGHT 64
#define DEQUE_INIT_CAP 1024
#define HEAP_INIT_CAP 1024
#define TAKEN 1
#define EMPTY 0
#define TOMB -1
#define clear(a,b) memset((a), 0, sizeof(a[0]) * (b))

#define N 100005

int stack[N], day[N], study[N];
bool visited[N];

bool checkit(int idx, int m) {
    clear(visited, N);
    visited[0] = true;
    int top = 0;
    forr (i, idx, 1) {
        if (!visited[day[i]]) {
            visited[day[i]] = true;
            stack[top++] = i;
        }
    }
    if (top != m) return false;
    int used = 0;
    forr (i, top - 1, 0) {
        int t = stack[i];
        int d = study[day[t]];
        if (t - 1 - used - d < 0) return false;
        used += d + 1;
    }
    return true;
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    fore (i, 1, n) scanf("%d", &day[i]);
    fore (i, 1, m) scanf("%d", &study[i]);

    int lo = 1; int hi = n;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (checkit(mid, m)) hi = mid - 1;
        else lo = mid + 1;
    }
    if (lo > n) lo = -1;
    printf("%d", lo);
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# define max(a,b) (a > b) ? a : b
# define min(a,b) (a < b) ? a : b
# define maxm 100010
int a[maxm],d[maxm],vis[maxm],n,m;
int judge(int day)
{
	int i,j,total = 0,sum = 0;
	memset(vis,0,sizeof(vis));
	for(i = day;i >= 1;i--){
		if(d[i] && !vis[d[i]]){
			sum += a[d[i]];
			vis[d[i]] = 1;
			total++;
		}
		else if(sum > 0)
			sum--;
	}
	if(sum > 0 || total != m)
		return 0;
	return 1;

}
int main()
{
	int i,j,ok = 0,ans = 1000000,l,r,mid;
	scanf("%d%d",&n,&m);
	for(i = 1;i <= n;i++)
		scanf("%d",&d[i]);
	for(i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	l = 1,r = n;
	while(l <= r){
		mid = (l+r) / 2;
		if(judge(mid)){
			ok  = 1;
			ans = min(ans,mid);
			r = mid - 1;
		}
		else
			l = mid + 1;
	}
	if(ok)
		printf("%d\n",ans);
	else
		printf("-1\n");
	return 0;
}


#include <stdio.h>

#define N	100000

int passed(int *aa, int *dd, int n, int m) {
	int i, k, l, ii[N], jj[N], used[N] = { 0 }, bb[N] = { 0 };

	k = m - 1;
	for (i = n - 1; i >= 0; i--)
		if (dd[i] > 0) {
			int j = dd[i] - 1;

			if (!used[j]) {
				used[j] = 1;
				ii[k] = i;
				jj[k--] = j;
			}
		}
	if (k > 0)
		return 0;
	k = 0;
	l = 0;
	for (i = 0; i < n; i++)
		if (l < m && ii[l] == i) {
			int j = jj[l];

			if (aa[j] != bb[j])
				return 0;
			l++;
		} else if (k < m) {
			int j = jj[k];

			bb[j]++;
			if (aa[j] == bb[j])
				k++;
		}
	return l == m;
}

int binsearch(int *aa, int *dd, int n, int m) {
	int low = 0, high = n + 1, mid;

	while (high - low > 1) {
		mid = (low + high) / 2;

		if (passed(aa, dd, mid, m))
			high = mid;
		else
			low = mid;
	}
	return high;
}

int main() {
	int i, n, m, k;
	static int dd[N], aa[N + 1];

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &dd[i]);
	for (i = 0; i < m; i++)
		scanf("%d", &aa[i]);
	k = binsearch(aa, dd, n, m);
	printf("%d\n", k == n + 1 ? -1 : k);
	return 0;
}


#include <stdio.h>

#define N	100000

int check(int *aa, int *dd, int n, int m) {
	int i, j, k, ee[N], ii[N + 1], cnt[N + 1] = { 0 };

	for (i = 0; i < n; i++)
		ii[i] = -1;
	k = 0;
	for (i = n - 1; i >= 0; i--)
		if (dd[i] > 0 && ii[dd[i]] == -1) {
			ii[dd[i]] = i;
			ee[k++] = dd[i];
		}
	if (k != m)
		return 0;
	j = k - 1;
	for (i = 0; i < n && j >= 0; i++) {
		if (ii[dd[i]] == i && ee[j] == dd[i])
			return 0;
		dd[i] = ee[j];
		cnt[ee[j]]++;
		if (cnt[ee[j]] == aa[ee[j]])
			j--;
	}
	return 1;
}

int binsearch(int *aa, int *dd, int n, int m) {
	int low = -1, high = n + 1, mid;

	while (high - low > 1) {
		mid = (low + high) / 2;
		if (check(aa, dd, mid, m))
			high = mid;
		else
			low = mid;
	}
	return high;
}

int main() {
	int i, n, m, k;
	static int dd[N], aa[N + 1];

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &dd[i]);
	for (i = 1; i <= m; i++)
		scanf("%d", &aa[i]);
	k = binsearch(aa, dd, n, m);
	printf("%d\n", k == n + 1 ? -1 : k);
	return 0;
}


#include <stdio.h>

#define N	100000

int check(int *aa, int *dd, int n, int m) {
	int i, j, k, ee[N], ii[N + 1], cnt[N + 1] = { 0 };

	for (i = 0; i < n; i++)
		ii[i] = -1;
	k = 0;
	for (i = n - 1; i >= 0; i--)
		if (dd[i] > 0 && ii[dd[i]] == -1) {
			ii[dd[i]] = i;
			ee[k++] = dd[i];
		}
	if (k != m)
		return 0;
	j = k - 1;
	for (i = 0; i < n && j >= 0; i++) {
		if (ii[dd[i]] == i && ee[j] == dd[i])
			return 0;
		dd[i] = ee[j];
		cnt[ee[j]]++;
		if (cnt[ee[j]] == aa[ee[j]])
			j--;
	}
	return 1;
}

int binsearch(int *aa, int *dd, int n, int m) {
	int low = 0, high = n + 1, mid;

	while (high - low > 1) {
		mid = (low + high) / 2;
		if (check(aa, dd, mid, m))
			high = mid;
		else
			low = mid;
	}
	return high;
}

int main() {
	int i, n, m, k;
	static int dd[N], aa[N + 1];

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &dd[i]);
	for (i = 1; i <= m; i++)
		scanf("%d", &aa[i]);
	k = binsearch(aa, dd, n, m);
	printf("%d\n", k == n + 1 ? -1 : k);
	return 0;
}


#include "bits\stdc++.h"
#include <cstring>
#define LL long long
#define max(a,b) (a>b)?(a):(b)
#define MAX 100000
LL m, n;
LL arr[MAX+1];
LL cost[MAX + 1];
LL marker[MAX + 1];
LL cost_total = 0;
LL left, right;
bool test(LL end) {
	if (end + m < cost_total)  return false;
	memset(marker, 0, sizeof(LL)*(m + 1));
	LL cnt_marker = 0;
	LL sum = 0;

	for (LL i = end; i >= 1; i--)
	{
		if (arr[i] && !marker[arr[i]]) {
			sum += cost[arr[i]];
			if (i - 1 < sum) return false;
			marker[arr[i]] = 1;
			cnt_marker++;
			if (cnt_marker == m) return true;
		}
		else {
			sum--;
			sum=max(sum, 0);
		}
	}

}
int main() {

	scanf("%ld %ld", &n, &m);
	for (LL i = 1; i <= n; i++)
	{
		scanf("%ld", &arr[i]);
	}
	for (LL i = 1; i <= m; i++)
	{
		scanf("%ld", &cost[i]);
		cost_total += cost[i];
	}

	left = 1;
	right = n;
	while (left <= right) {
		LL mid = (left + right) / 2;
		if (test(mid)) {
			right = mid - 1;
		}
		else {
			left = mid + 1;
		}
	}
	if (left <= n) {
		printf("%ld\n", left);
	}
	else {
		printf("-1\n");
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#define LL long long
#define max(a,b) (a>b)?(a):(b)
#define MAX 100000
LL m, n;
LL arr[MAX+1];
LL cost[MAX + 1];
LL marker[MAX + 1];
LL cost_total = 0;
LL left, right;
LL cnt_marker = 0;
LL sum = 0;
bool test(LL end) {
	cnt_marker = 0; sum = 0;
	if (end + m < cost_total)  return false;
	memset(marker, 0, sizeof(LL)*(m + 1));
	for (LL i = end; i >= 1; i--)
	{
		if (arr[i] && !marker[arr[i]]) {
			sum += cost[arr[i]];
			if (i - 1 < sum) return false;
			marker[arr[i]] = 1;
			cnt_marker++;
			if (cnt_marker == m) return true;
		}
		else {
			sum--;
			sum=max(sum, 0);
		}
	}

}
int main() {
	LL i;
	scanf("%ld %ld", &n, &m);
	for (i = 1; i <= n; i++)
	{
		scanf("%ld", &arr[i]);
	}
	for (i = 1; i <= m; i++)
	{
		scanf("%ld", &cost[i]);
		cost_total += cost[i];
	}

	left = 1;
	right = n;
	while (left <= right) {
		LL mid = (left + right) / 2;
		if (test(mid)) {
			right = mid - 1;
		}
		else {
			left = mid + 1;
		}
	}
	if (left <= n) {
		printf("%ld\n", left);
	}
	else {
		printf("-1\n");
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#define LL long long
#define max(a,b) (a>b)?(a):(b)
#define MAX 100000
LL m, n;
LL arr[MAX+1];
LL cost[MAX + 1];
LL marker[MAX + 1];
LL cost_total = 0;
LL left, right;
LL cnt_marker = 0;
LL sum = 0;
int test(LL end) {
	cnt_marker = 0; sum = 0;
	if (end + m < cost_total)  return 0;
	memset(marker, 0, sizeof(LL)*(m + 1));
	for (LL i = end; i >= 1; i--)
	{
		if (arr[i] && !marker[arr[i]]) {
			sum += cost[arr[i]];
			if (i - 1 < sum) return 0;
			marker[arr[i]] = 1;
			cnt_marker++;
			if (cnt_marker == m) return 1;
		}
		else {
			sum--;
			sum=max(sum, 0);
		}
	}
	return 0;

}
int main() {
	LL i; LL j;
	scanf("%ld %ld", &n, &m);
	for (i = 1; i <= n; i++)
	{
		scanf("%ld", &arr[i]);
	}
	for (i = 1; i <= m; i++)
	{
		scanf("%ld", &cost[i]);
		cost_total += cost[i];
	}

	left = 1;
	right = n;
	while (left <= right) {
		LL mid = (left + right) / 2;
		if (test(mid)) {
			right = mid - 1;
		}
		else {
			left = mid + 1;
		}
	}
	if (left <= n) {
		printf("%ld\n", left);
	}
	else {
		printf("-1\n");
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#define LL long long
#define max(a,b) (a>b)?(a):(b)
#define MAX 100000
LL m, n;
LL arr[MAX+1];
LL cost[MAX + 1];
LL marker[MAX + 1];
LL cost_total = 0;
LL left, right;
LL cnt_marker = 0;
LL sum = 0;
int test(LL end) {
	cnt_marker = 0; sum = 0;
	if (end + m < cost_total)  return 0;
	memset(marker, 0, sizeof(LL)*(m + 1));
	for (LL i = end; i >= 1; i--)
	{
		if (arr[i] && !marker[arr[i]]) {
			sum += cost[arr[i]];
			if (i - 1 < sum) return 0;
			marker[arr[i]] = 1;
			cnt_marker++;
			if (cnt_marker == m) return 1;
		}
		else {
			sum--;
			sum=max(sum, 0);
		}
	}
	return 0;

}
int main() {
	LL i;
	scanf("%ld %ld", &n, &m);
	for (i = 1; i <= n; i++)
	{
		scanf("%ld", &arr[i]);
	}
	for (i = 1; i <= m; i++)
	{
		scanf("%ld", &cost[i]);
		cost_total += cost[i];
	}

	left = 1;
	right = n;
	while (left <= right) {
		LL mid = (left + right) / 2;
		if (test(mid)) {
			right = mid - 1;
		}
		else {
			left = mid + 1;
		}
	}
	if (left <= n) {
		printf("%ld\n", left);
	}
	else {
		printf("-1\n");
	}
	return 0;
}


#include <stdio.h>

const int maxn = 1e5;

int passes(int *day, int *exam, int n, int m)
{
    int p[n], o[m];
    int i;

    for (i =0; i<m; i++) o[i] = 0;

    for (i = n-1; i>=0; i--)
        if (day[i] == 0) p[i] = 0;
        else if (o[day[i]-1] == 1) p[i] = 0;
        else
        {
            o[day[i]-1] = 1;
            p[i] = day[i];
        }

   /* for (i =0; i<n; i++) printf("%d ", p[i]);
    printf("\n");
    for (i =0; i<m; i++) printf("%d ", o[i]);
    printf("\n");
*/
    int pom = 0;
    for (i = 0; i<m; i++)
        if (o[i]) pom++;

    if (pom != m) return 0;

    pom = 0;

    for (i = 0; i<n; i++)
    {
        if (p[i] == 0) pom++;
        else
        {
            pom -= exam[p[i]-1];
            if (pom<0) return 0;
        }
    }

    return 1;
}

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

    int i;

    int *day, *exam;

    day = (int *)malloc(maxn*sizeof(int));
    exam = (int *)malloc(maxn*sizeof(int));

    for (i = 0; i<n; i++) scanf("%d", &day[i]);

    for (i = 0; i<m; i++) scanf("%d", &exam[i]);

    if (!passes(day,exam,n,m))
    {
        printf("-1");
        return 0;
    }

    int l = 1;
    int r = n;

    while(l<r)
    {
        int mid = (l+r)/2;

        //printf("%d %d\n", mid, passes(day,exam,mid,m));

        if (passes(day,exam,mid,m)) r = mid;
        else l = mid+1;

    }

    printf("%d", (l+r)/2);

}


#include<stdio.h>
#include<math.h>

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main()
{
    int n,m,i,j,d[100000],a[100000],c[100000],k;
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
        scanf("%d",&d[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i]);
    }
    qsort(a,m,sizeof(int), cmpfunc);
    int u = 0,flag = 0;
    k=0;
    for(i=0;i<n;i++)
    {
        if(d[i] == 0)
        {
            k++;
        }
        else
        {
            if(k >= a[u])
            {
                k = k-a[u];
                u++;
            }
            else
            {
                k++;
            }
        }
        if(u >= m)
        {
            flag = 1;
            break;
        }
    }
    if(flag == 1)
    {
        printf("%d\n",i+1);
    }
    else
    {
        printf("-1\n");
    }
    return 0;
}


#include<stdio.h>
typedef long long unsigned llu;
typedef unsigned u;
u D[111111],A[111111],V[111111],Vi;
u F(u n)
{
	++Vi;u i,j=0;llu r=0;
	for(i=n;i--;)
	{
		if(D[i]&&V[D[i]]!=Vi)
		{
			V[D[i]]=Vi;
			r+=A[D[i]];++j;
			continue;
		}
		if(r)--r;
	}
	return j-(r>0);
}
int main()
{
	u n,m,i=-1,j,k;
	for(scanf("%u%u",&n,&m);++i<n;)scanf("%u",D+i);
	for(i=0;++i<=m;)scanf("%u",A+i);
	for(i=0,j=n+1;(k=(i+j)>>1)>i;)
	{
		if(F(k)==m)j=k;
		else i=k;
	}
	printf(j>n?"-1\n":"%u\n",j);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            last=cost[days[i]];
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
    }
    if(count==m)
        return 1;
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int main()
{
    int days[N],cost[N];
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        qsort(cost+1,m,sizeof(cost[0]),cmp);
        int cur=1;
        int cnt=0;
        int ans=-1;
        for(int i=1;i<=n;i++)
        {
            if(days[i]==0)
                cnt++;
            else
            {
                if(cnt>=cost[cur]) cnt-=cost[cur++];
                else cnt++;
            }
            if(cur==m+1)
            {
                ans=i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(temp>last) cnt-=(temp-last);
            last=cost[days[i]];
            temp=0;
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<right)
        {
            int mid=(left+right)/2;
            if(judge(mid)) ans=right=mid;
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(temp>last) cnt-=(temp-last);
            last=cost[days[i]];
            temp=0;
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right) break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            //if(temp>last) cnt-=(temp-last);
            last=cost[days[i]];
            temp=0;
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid-1;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            //if(temp>last) cnt-=(temp-last);
            last=cost[days[i]];
            temp=0;
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right) break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(temp>last) cnt-=(temp-last);
            temp=0;
            last=cost[days[i]];
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(temp>last) cnt-=(temp-last);
            last=cost[days[i]];
            temp=0;
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<right)
        {
            int mid=(left+right)/2;
            if(judge(mid)) ans=right=mid;
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
            if(count==m) break;
            last+=cost[days[i]];
        }
    }
    if(count==m&&cnt>=(temp-last))
        return 1;
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            last+=cost[days[i]];
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m && temp>=last)
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            last+=cost[days[i]];
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m && cnt>=(temp-last))
            return 1;
        else if(count==m)
            return 0;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
            if(count==m) break;
            last=cost[days[i]];
        }
    }
    if(count==m&&cnt>=(temp-last))
        return 1;
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
int days[N],cost[N],mark[N];
int n,m;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int judge(int target)
{
    int cnt=target;
    int temp=0;
    int last=0;
    int count=0;
    memset(mark,0,sizeof(mark));
    for(int i=target;i>=1;i--)
    {
        if(days[i]==0 || mark[days[i]]!=0)
            temp++;
        else
        {
            last+=cost[days[i]];
            if(cnt>cost[days[i]])
            {
                cnt-=(cost[days[i]]+1);
            }else
                return 0;
            mark[days[i]]=1;
            count++;
        }
        if(count==m && cnt>=(temp-last))
            return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&days[i]);
        for(int i=1;i<=m;i++) scanf("%d",&cost[i]);
        int left,right,ans;
        left=1,right=n,ans=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(judge(mid))
            {
                ans=mid;
                right=mid;
                if(left==right)
                    break;
            }
            else left=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

#define MAX 200010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

bool visited[MAX];
int n, m, ar[MAX], days[MAX], counter[MAX];

bool check(int t){
    clr(visited);
    clr(counter);
    int i, j, k, l, x, free = 0;

    for (i = 1; i <= t; i++){
        if (ar[i]) counter[ar[i]]++;
    }

    for (i = 1; i <= t; i++){
        if (ar[i] == 0) free++;
        else{
            if (counter[ar[i]] > 1) free++;
            else{
                if (free < days[ar[i]]) return false;
                free -= days[ar[i]];
                visited[ar[i]] = true;
            }
            counter[ar[i]]--;
        }
    }

    for (i = 1; i <= m && visited[i]; i++){};
    return (i > m);
}

int main(){
    int i, j, k, l, low, high, mid, res;

    while (scanf("%d %d", &n, &m) != EOF){
        for (i = 1; i <= n; i++) scanf("%d", &ar[i]);
        for (i = 1; i <= m; i++) scanf("%d", &days[i]);

        res = -1;
        low = 1, high = n;
        while ((low + 1) < high){
            mid = (low + high) >> 1;
            if (check(mid)) high = mid;
            else low = mid;
        }

        if (check(low)) res = low;
        else if (check(high)) res = high;

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


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m, i;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int flag;
	char push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	for(i=1;i<=m;i++)	mArr[i].spentDays=0;
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);	sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				push(stack, &mArr[i]);	push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);	sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	if(push_check[1])	flag = stack_empty(stack);
	else flag = 1;
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	scanf("%d", &nArr[i]);
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		total+=mArr[i].neededDays;
	}
	total+=m;	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;	end = mid-1;
		}
		else	start = mid+1;
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	while(pos>=1)	{
		i = nArr[pos];
		if(i)	{
			if(mArr[i].spentDays == mArr[i].neededDays && stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
			else	{
				if(push_check[i] && stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
				else	{
					push(stack, &mArr[i]);
					push_check[i] = 1;
				}
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	flag = stack_empty(stack);
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		mArr[i].spentDays = 0;
		total+=mArr[i].neededDays;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			end = mid-1;
			pos = mid;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				push(stack, &mArr[i]);
				push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	free_stack(stack);
	for(i=1, flag=0; i<=m; i++)	{
		if(mArr[i].spentDays<mArr[i].neededDays){
			flag=1;	break;
		}
	}
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		mArr[i].spentDays = 0;
		total+=mArr[i].neededDays;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;
			end = mid-1;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				push(stack, &mArr[i]);
				push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	for(i=1, flag=0; i<=m; i++)	{
		if(mArr[i].spentDays<mArr[i].neededDays){
			flag=1;	break;
		}
	}
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		mArr[i].spentDays = 0;
		total+=mArr[i].neededDays;
	}
	if(n == 100000)	{
		printf("val: %d\n",mArr[1]);
		return 0;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;
			end = mid-1;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m, i;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int flag;
	char push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);	sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				mArr[i].spentDays=0;	push(stack, &mArr[i]);	push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);	sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	if(push_check[1])	flag = stack_empty(stack);
	else flag = 1;
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	scanf("%d", &nArr[i]);
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		total+=mArr[i].neededDays;
	}
	total+=m;	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;	end = mid-1;
		}
		else	start = mid+1;
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	for(i=1;i<=m;i++)	mArr[i].spentDays=0;
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				push(stack, &mArr[i]);
				push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	if(push_check[1])	flag = stack_empty(stack);
	else flag = 1;
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		total+=mArr[i].neededDays;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;
			end = mid-1;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	for(i=1;i<=m;i++)	mArr[i].spentDays=0;
	while(pos>0)	{
		i = nArr[pos];
		if(i)	{
			if(push_check[i])	{
				if(stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
			}
			else	{
				push(stack, &mArr[i]);
				push_check[i] = 1;
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	flag = stack_empty(stack);
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		total+=mArr[i].neededDays;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			pos = mid;
			end = mid-1;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define MAX 100010
#define STACKDATATYPE __exam *

struct st_exam	{
	int neededDays;
	int spentDays;
};
typedef struct st_exam __exam;

int nArr[MAX];
int n, m;
int i, j, k;
__exam mArr[MAX];

struct st_stack	{
	STACKDATATYPE *container;
	int stackSize;
	int topPos;
	int count;
};
typedef struct st_stack __stack;

__stack *create_stack(int size)	{
	__stack *sp;
	sp = (__stack *)malloc(sizeof(__stack));
	if(sp == NULL)	return NULL;
	stack_flash(sp);
	sp->stackSize = size;
	sp->container = (STACKDATATYPE *)malloc(size*sizeof(STACKDATATYPE));
	if(sp->container == NULL)	return NULL;
	return sp;
}
int stack_flash(__stack *sp)	{
	sp->topPos = 0;	sp->count = 0; return 0;
}
int push(__stack *sp, STACKDATATYPE data)	{
	if(sp->topPos == sp->stackSize)	return 1;
	++sp->count;	sp->container[sp->topPos++] = data;
	return 0;
}
int pop(__stack *sp)	{
	if(sp->topPos == 0)	return 1;
	--sp->count;	--sp->topPos;	return 0;
}
STACKDATATYPE top(__stack *sp)	{
	if(sp->topPos == 0)	return sp->container[sp->topPos];
	return sp->container[(sp->topPos-1)];
}
int stack_empty(__stack *sp)	{
	return sp->topPos;
}
int free_stack(__stack *sp)	{
	free(sp->container);	free(sp);	return 0;
}

int check_pos(int pos)	{
	int i, flag, push_check[MAX]={0};
	__exam *sp;
	__stack *stack;
	stack = create_stack(m);
	while(pos>=0)	{
		i = nArr[pos];
		if(i)	{
			if(mArr[i].spentDays == mArr[i].neededDays && stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
			else	{
				if(push_check[i] && stack_empty(stack))	{
					sp = top(stack);	pop(stack);
					sp->spentDays++;
					if(sp->spentDays<sp->neededDays)	push(stack, sp);
				}
				else	{
					push(stack, &mArr[i]);
					push_check[i] = 1;
				}
			}
		}
		else	{
			if(stack_empty(stack))	{
				sp = top(stack);	pop(stack);
				sp->spentDays++;
				if(sp->spentDays<sp->neededDays)	push(stack, sp);
			}
		}
		pos--;
	}
	flag = stack_empty(stack);
	free_stack(stack);
	return flag;
}
int main()	{
	int start, end, mid, total, pos;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++)	{
		scanf("%d", &nArr[i]);
	}
	for(i=1, total=0; i<=m; i++)	{
		scanf("%d", &mArr[i].neededDays);
		mArr[i].spentDays = 0;
		total+=mArr[i].neededDays;
	}
	total+=m;
	start = total;	end = n;	pos = 0;
	while(start<=end)	{
		mid = (start+end)/2;
		if(check_pos(mid) == 0)	{
			end = mid-1;
			pos = mid;
		}
		else	{
			start = mid+1;
		}
	}
	if(pos)	printf("%d\n", pos);
	else	puts("-1");
	return 0;
}


#include<stdio.h>
int isvalid(int days[],int prep[],int m,int x)
{
    int i,subdone[x],chkarr[m],buc[m],s=0;
    for(i=0;i<m;++i)
        buc[i]=0;
    for(i=0;i<x;++i)
        subdone[i]=0;
    int count=0;
    for(i=x-1;i>=0;--i)
    {
        if (days[i]>0 && buc[days[i]-1]==0)
        {
            buc[days[i]-1]=i+1;
            subdone[i]=1;
            chkarr[count]=prep[days[i]-1];
            count+=1;
        }

        if (count==m)
            break;
    }

    if (count!=m)
        return 0;
    count=m-1;

    for(i=0;i<x;++i)
    {

            if (chkarr[count]==0)
             {
                count--;
                if(count<0)
                    break;
             }
        if (subdone[i]==0)
            chkarr[count]--;

        else
        {
        s+=1;
        if(count==m-s && chkarr[count]>0)
                return 0;
        }

    }
    return 1;
}
int main()
{
    int n,m,i;
    scanf("%d%d",&n,&m);
    int d[n],a[m];
    for(i=0;i<n;++i)
        scanf("%d",&d[i]);
    for(i=0;i<m;++i)
        scanf("%d",&a[i]);
    if(m>n)
        printf("-1\n");
    else{
        int lo=0,ans=-1,hi=n;
        int mid=lo+(hi-lo)/2;
        while(lo<=hi)
        {

            if (isvalid(d,a,m,mid))
            {
                ans=mid;
                hi=mid-1;
            }
            else lo=mid+1;
            mid=lo+(hi-lo)/2;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<stdio.h>
int n,m;
int a[10010],b[10010],c[10010];
int check(int k);
int main()
{
	int i,mi=0,mid,f,e,ans=-1;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=m;i++)
	{
		scanf("%d",&b[i]);
		c[i]=0;
	}
	f=1;e=n+1;
	mid=(f+e)/2;
	while(mi!=mid)
	{
		if(check(mid))
		{
			e=mid;
			ans=mid;
		}
		else
			f=mid;
		mi=mid;
		mid=(f+e)/2;
	}
	printf("%d",ans);
	return 0;
}

int check(int k)
{
	int i,s=0,j;
	for(i=k;i>0;i--)
	{
		if(c[a[i]]||!a[i])
		{
			if(s)
				s--;
			continue;
		}
		else
		{
			c[a[i]]=1;
			s+=b[a[i]];
		}
	}
	for(i=1;i<=m;i++)
	{
		if(!c[i])
			s=1;
		c[i]=0;
	}

	if(s>0)
		return 0;
	else
		return 1;
}